Unknown provider: $modalInstanceProvider <- $mo

2019-08-15 03:28发布

问题:

I am using angular bootstrap ui modal which is working fine but when I try to close it using modalInstance it gives above error.Here is my code

  var app = angular.module('LoginModule', ['ui.bootstrap']);
app.controller('LoginModal', ['$scope',  '$modal', function ($scope, $modal) {
    $scope.animationsEnabled = true;
    $scope.open = function (size) {

        var modalInstance = $modal.open({
            animation: $scope.animationsEnabled,
            templateUrl: '/app/template/Login.html',
            controller: 'LoginController',
            size: size
        });
    }
}]);
app.controller('LoginController', ['$scope', '$modalInstance', '$http', function ($scope, $modalInstance, $http) {
    $scope.model = {};
    $scope.loading = {
        state: false
    }
    $scope.errors = '';
    $scope.email = "";
    $scope.cancel = function () {

        $modalInstance.dismiss('cancel');
    };
}]);

I have created the cancel function in the controller which I specify with the template still it gives error.I use ng-click="cancel()" in button inside LoginController . Need help?

回答1:

Looks like you are instantiating the controller with ng-controller directive in the modal view. Instead you only need to use controller option of the modal in order to get the special dependency $modalInstance injected. If you have instantiated controller using ng-controller="LoginController" you need to remove it and you would not need it as well as the controller would automatically be instantiated (by resolving special dependency $modalInstance) and attached to the template.