angular.js:13424 Error: [$injector:unpr]

angular.js:13424 Error: [$injector:unpr]

本文关键字:injector unpr Error js 13424 angular      更新时间:2024-06-23

我正在使用angular ui引导库来实现引导模式-请参阅此处。

一切都很好,模态正在打开,但不会关闭。我曾尝试使用$uibModalInstance,但当我尝试关闭模态时,根据上面链接中的示例,我收到以下消息:

angular.js:13424错误:[$injector:unp]http://errors.angularjs.org/1.5.3/$injector/unp?p0=%24uibModalInstanceProvider%20%3C-%20%24uibModal Instance%20%3C-%20testCtrl

我使用的是Angular 1.5.3版本,ui bootstrap tpls v1.3.1和bootstrap css 3.3.6

我的代码如下所示。我已经尝试过处理同一问题的所有答案,但无法解决这个问题。我认为angular版本有问题,但在我去玩angular版本之前,我想确保没有遗漏任何东西。我原以为这个答案会奏效,但没有这样的运气。

app.js

var app = angular.module('testApp', ['ui.bootstrap'])
.config(function($routeProvider) {
    $routeProvider. 
       when('/', {
         templateUrl: 'app/views/address-book.html',
         controller: 'testCtrl as ctrl'
       }).
       otherwise({
         redirectTo: '/'
       });
})
.controller('testCtrl', ['$uibModal', '$uibModalInstance', function ($uibModal, $uibModalInstance) {
    this.open = function () {
        testCtrl.modalInstance = $uibModal.open({
          templateUrl: 'app/views/partials/form.html',
          controller: 'testCtrl as ctrl'
        });
    }
    this.close = function() {
        console.log(testCtrl.modalInstance) //this shows undefined
        testCtrl.modalInstance.close();
    }
}]);

html

<a class="enterNewAddressBtn" ng-click="ctrl.open()">Enter new address</a>

form.html

<form>
    <input ng-model="ctrl.name" type="text">
    <button class="cancel-btn" ng-click="ctrl.close()">Cancel</button>
</form>

显示此错误是因为您希望在创建模式实例之前注入$uibModalInstanceProvider。如果您使用不同的控制器并在模式实例控制器中注入$uibModalInstance,则此错误将被删除。

$uibModal$uibModalInstance最好使用不同的控制器。$uibModalInstance表示模式窗口(实例)依赖关系—它与$uibModal服务不同。使用$uibModal模式打开,使用$uibModalInstance模式关闭。

你可以从这个问题中获得想法,更多细节可以访问这里的plunker示例。

它可能会帮助您

如果我没有记错,在使用$uibModal.open()创建模态之前,您不能注入$uibModalInstance;

将$uibModalInstance的注入移动到touchnoteCtrl,它应该可以正常工作。