Call the parent controller method from the child modal controller (ui bootstrap)

So here is the im condition that is having problems, it looks like I have a parent controller and a child controller that is a modal controller, I have a method in the parent controller that I want to call from a child modal controller, I don’t know what im missing, but this is what I tried.

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 ==========>") } 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']; }]); }]); 

but it gives me an error, not such a function, I get an error on the verification method, I want to call this verification method from the modal instance controller, but could not do it, please help.

+5
source share
2 answers

https://angular-ui.imtqy.com/bootstrap/#/modal

The modal in bootstrap has a "scope" parameter,

scope - an instance of the scope that will be used for modal content (in fact, the $ modal service will create a child scope of the provided scope). Default is $ rootScope

using scope: $scope , you should use the methods defined in the parent controller

example:

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

Allow $ scope to $ modal and just call $ scope.parent.check and you're done!

+1
source

Source: https://habr.com/ru/post/1232961/


All Articles