2016-06-21 107 views
1

我想在執行動作之前向用戶顯示一個彈出式警告模式(在這種情況下刪除)。如果他們點擊模型中的okay,它會繼續,如果他們點擊cancel,它會執行其他操作。Angular modal return promise

要做到這一點,我試圖通過模式承諾沿着鏈回到最初呼籲$rootScope.openDirections('warning').then(...,所以我可以決定在那裏做什麼。

但是我得到錯誤:

$ rootScope.openDirections(...),那麼是不是一個函數at Scope.$scope.deleteObj

告訴我,不返回的承諾:

$scope.deleteObj = function (id, type) { 
     $rootScope.openDirections('warning').then(function (res) { 
      // Do Stuff 
     }, function (err) { 
      console.log('ERROR', err); 
     }); 

這調用一個$ rootScope函數:(我看到這裏的對象類型是一個承諾...所以不應該能夠現在返回到原來的調用者?)

$rootScope.openDirections = function (type) { 
     // A Promise: Object {result: Promise, opened: Promise, rendered: Promise} 
     return Modal.showDirections(type); 
    }; 

其中要求Modal工廠開方向模態:

 return { 
      ... 
      showDirections: function(type) { 
       return $modal.open({ 
        backdrop: 'static', 
        templateUrl: 'modal.html', 
        controller: 'directionsCtrl', 
        resolve: { 
         // Display custom modal template 
         obj: function() { 
          if (type === 'markdown') { 
           return { 
            template: '/modals/directions.html' 
           }; 
          } else { 
           return { 
            template: '/modals/message.html' 
           }; 
          } 
         }, 
         testID : function() { 
          return 'WHAT DO I RETURN HERE?'; 
         } 
        } 
       }); 
      } 

這使得message.html模式的模板:

<div class="admin-directions"> 
    <div class="directions"> 
     ARE YOU SURE YOU WANT TO DELETE THIS? 
    </div> 
    <p class="dismiss-admin-help" ng-click="okay()">Okay</p> 
    <p class="dismiss-admin-help" ng-click="cancel()">Cancel</p> 
</div> 

這對於模態使用directionsCtrl

angular 
    .module('DDE') 
    .controller('directionsCtrl', ['$rootScope', '$scope', '$modalInstance', 'Modal', 'obj', 'Auth', 'VARS', '$route', '$location', 'testID', 
     function ($rootScope, $scope, $modalInstance, Modal, obj, Auth, VARS, $route, $location, testID) { 

      $scope.template = obj.template; 

      $scope.testID = ''; 

      /** 
      * Dismiss modal 
      */ 
      $scope.ok = function() { 
       $scope.testID = 'okay'; 
       return $modalInstance.result($scope.testID); 
      }; 

      $scope.cancel = function() { 
       $scope.testID = 'cancel'; 
       return $modalInstance.result($scope.testID); 
      }; 

    }]); 

回答

1

$ modal.open返回一個$ modalInstanc e有一個名爲結果的屬性,這是承諾。你必須調用.then()。所以你的情況,你想這樣:

$rootScope.openDirections('warning').result.then(function (res) { 
     // Do Stuff 
    }, function (err) { 
     console.log('ERROR', err); 
    }); 

或者,在你的openDirections方法,你可以只歸還承諾:

 showDirections: function(type) { 
      return $modal.open({ 
       //all your current stuff stays the same here 
      }).result; //<-- notice that you are now returning the promise 
     } 
+0

好了,我的'$ modal'實例不是在同一控制器,而是在自己的工廠。因此,我在工廠內完全「解析」什麼以回傳給原始呼叫者? – Growler

+0

你是什麼意思?當你做了我建議的事情後,你的錯誤消失了嗎? – mcgraphix

+0

'$ scope.ok'和'$ scope.cancel'按鈕不再適用於模態 – Growler