如何从角度js另一个控制器内调用ui bootstrap模态弹出控制器

时间:2015-10-05 11:21:57

标签: angularjs angular-ui-bootstrap

如何从另一个控制器内部调用angular ui bootstrap模式弹出窗口,因为条件就像是从视图中调用而我需要从函数内部调用它

       App.controller('MailFolderController', ['$scope', '$http', '$timeout', '$stateParams', '$window', 'mails', '$interval', function ($scope, $http, $timeout, $stateParams, $window, mails, $interval) {


     $scope.check = function(){
console.log("call parent ==========>")
     // call open method in modal popup here  
   }


    App.controller('orderCancellationController', ['$scope', '$modal', function ($scope, $modal) {

$scope.open = function (mail) {
    var modalInstance = $modal.open({
        templateUrl: '/orderCancellationBox.html',
        controller: ModalInstanceCtrl,
        resolve: {
            mail: function () {
                return mail;
            }
        }
    });
};

// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.

var ModalInstanceCtrl = function ($scope, $modalInstance, mail) {

    $scope.mail = mail;
    $scope.submit = function () {
        $scope.$parent.check();
        $modalInstance.close('closed');
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
};
ModalInstanceCtrl.$inject = ["$scope", "$modalInstance", 'mail'];

 }]);


}]);

所以我想从check方法中调用orderCancellationController中的open方法,帮助!!

1 个答案:

答案 0 :(得分:2)

根据我的评论示例:options for GCC

为了从另一个控制器打开模态,你必须创建一个服务,我在app.js文件中这样做了,如下所示:

myApp.service('modalProvider',['$modal', function ($modal) {

this.openPopupModal = function () {
    var modalInstance = $modal.open({
        templateUrl: '/orderCancellationBox.html',
        controller: 'ModalInstanceCtrl'
    });
};
}]);

然后在控制器中我想打开我的模态,我注入了'模态提供者'服务,像这样:

 App.controller('MailFolderController', ['$scope', '$http', '$timeout', '$stateParams', '$window', 'mails', '$interval','modalProvider', function ($scope, $http, $timeout, $stateParams, $window, mails, $interval, modalProvider) {

// function to open modal
 $scope.check = function(){
    modalProvider.openPopupModal();
}