简体   繁体   English

如何从Angular JS内调用UI Bootstrap模式弹出控制器

[英]How to call ui bootstrap modal popup controller from within angular js another controller

how can i call angular ui bootstrap modal popup from inside another controller , as the condition is like instead of calling from view i need to call it from inside a function 我如何从另一个控制器内部调用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'];

 }]);


}]);

so i want to call the open method in orderCancellationController from inside the check method , help !! 所以我想从check方法内部调用orderCancellationController中的open方法,帮助!

Following the example from my comment: Angular Calling Modal Open function from one controller to another 遵循我评论中的示例: 从一个控制器到另一个控制器的Angular调用Modal Open函数

In order to open the modal from another controller you have to create a service, I did so in my app.js file, like so: 为了从另一个控制器打开模式,您必须创建一个服务,我在app.js文件中这样做,如下所示:

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

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

Then in the controller I wish to open my modal from, I inject the 'modalProvider' service, like so: 然后在控制器中,我希望从中打开我的模态,然后注入“ modalProvider”服务,如下所示:

 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM