How to enter "$ modalInstance" into the controller?

In my application, after I found out that the user is not registered, I want to open a modal dialog:

.when('/showtask/:id', {templateUrl: 'Home/Template/showtask', resolve: ShowTaskCtrl.resolve, access: { allowAnonymous: false }, resolve: { userAuthenticated: ["$http", "$q", function ($http, $q) { var deferred = $q.defer(); $http.get('/api/Authentication/UserAuthenticated').then(function (data) { if (data.data != "null") { deferred.resolve(data.data); } else { var modalInstance = { templateUrl: 'Home/Template/loginfailed', controller: 'ModalInstanceCtrl', modalpart: ['modalpart', function (modalpart) { return modalInstance; }] }; $modal.open(modalInstance); deferred.reject(); } }); return deferred.promise; }] } 

Since this happens when changing the route, I have to enter modalpart inside the instance and get it in the controller.

 var ModalInstanceCtrl = WorkerApp.controller('ModalInstanceCtrl', ["$scope", "modalpart", function ($scope, modalpart) { 

But I keep getting this error:

Unknown provider: modalpartProvider <- modalpart

How can I solve this problem?

PS The source code I'm looking at is here: http://angular-ui.imtqy.com/bootstrap/ (under modal)

+8
angularjs angular-ui
source share
3 answers

I have never had to use the $modal service inside a route change, like what you are doing, so I don’t know how this will work. However, just looking at your $modal code, it looks wrong. The right way:

 var modalOptions = { templateUrl: 'Home/Template/loginfailed', controller: 'ModalInstanceCtrl' }; $modal.open(modalOptions); 

And then your controller definition:

 WorkerApp.controller('ModalInstanceCtrl', ["$scope", "$modalInstance", function ($scope, $modalInstance) { // Do your stuff }]); 

The $modal will automatically handle the $modalInstance injection on your controller as per the documentation.

+8
source share

Had this problem for a while. Although you did not post your HTML, this was my problem. Make sure you do not name the controller in the DOM, this has already been taken care of by you in $ modal.open ().

+8
source share

I have the same problem and I found that the controller that I registered starts up twice! thanks @compguy, I deleted the controller name in the template. I Successd!

+2
source share

All Articles