2016-06-21 106 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