1

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

       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 !!

4
  • Take a look at this question -> stackoverflow.com/questions/28086854/… Commented Oct 5, 2015 at 11:26
  • 1
    My first suggestion would be to create a service that handles your modal open service if you don't want to copy paste your code. Commented Oct 5, 2015 at 11:31
  • @NotKnown How Armin already said, take the code for modal dialog in a service. Commented Oct 5, 2015 at 11:37
  • can anybody please elaborate , i m not getting exactly , like how the orderCancellatioController will bind Commented Oct 5, 2015 at 12:06

1 Answer 1

2

Following the example from my comment: Angular Calling Modal Open function from one controller to another

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:

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:

 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();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.