使用角度引导时出现$injector错误($modalInstanceProvider<-$modalInstan
$injector error when using angular-bootstrap ($modalInstanceProvider <- $modalInstance)
我一直在处理这个bug,但我似乎不明白。当我将添加到prod服务器的角度引导模型推送时,问题就开始了。最初的错误是:
"AngularJS错误:未知提供程序:aProvider<-a"
我确信我收到了那个错误,因为我的文件没有正确缩小。所以我检查了我的控制器,发现我的控制器中没有$injecting $modal
实例,这就是我遇到这个问题的时候。
每当我以缩小的格式将$modalInstance
注入控制器时,我都会收到这个错误。我没有使用angular bootstrap建议的格式,因为我正在构建的网站上有很多控制器,所以我把所有的东西都组合成一个控制器,而不是几个功能。
我的控制器:
.controller('CreateGroupCtrl', ['$scope', '$http', '$window', '$cookies', '$modal', '$log', 'FeedService', '$modalInstance',
function CreateGroupCtrl($scope, $http, $window, $cookies, $modal, $log, $modalInstance, FeedService) {
$scope.createGroupCall = function createGroupCall(teacher, name) {
if(teacher != null && name != null) {
FeedService.createGroupCall($cookies.token, $window.sessionStorage.user, teacher, name).success(function(data) {
console.log('GroupCreated');
}).error (function(status,data,token) {
console.log(status);
console.log(data);
});
} else {
alert("Error!");
}
}
/***********ANGULAR-UI MODAL CODE**********/
$scope.open = function (size) {
var modalInstance = $modal.open({
templateUrl: 'CreateGroupContent.html',
controller: CreateGroupCtrl,
size: size
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
$scope.ok = function () {
$modalInstance.close();
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
}]);
我的模板:
<button ng-controller="CreateGroupCtrl" ng-click="open()" type="button" id="creategroup" class="btn ns-btn">
<img class="ns-add" src="images/createGroup.png">
<p class="create">Create Group</p>
</button>
<div>
<script type="text/ng-template" id="CreateGroupContent.html">
<div class="modal-header">
<h2 class="modal-title ns-modal-title">Create A Group</h2>
<button class="ns-modal-close" ng-click="cancel()"><img src="images/xCancel.png"></button>
</div>
<div class="modal-body">
<form class="form-signin" role="form">
<input type="text" class="form-control ns-modal-form" placeholder="Teacher" ng-model="create.teacher" required autofocus>
<input type="text" class="form-control ns-modal-form" placeholder="Group Name" ng-model="create.name" required>
</form>
</div>
<div class="modal-footer">
<button class="btn ns-modal-add ns-btn" ng-click="createGroupCall(create.teacher, create.name); ok();" type="submit">Create</button>
</div>
</div>
</script>
</div>
首先,您需要按顺序注入所有内容。
此外,您应该将$modal
注入到要在其中创建模态视图的控制器中。并且$modalInstance
可以被注入ONLY到用于该$modal
窗口的控制器中。在您的情况下,您使用相同的控制器,因此无法注入$modalInstance
演示:http://plnkr.co/edit/khzNQ0?p=preview
此外,在您的情况下(当您只使用一个控制器时),您可以将字段scope
作为对象传递,该字段将用作模式视图的$scope
的父字段。默认情况下是$rootScope
,但您可以键入:
$scope.open = function (size) {
var modalInstance = $modal.open({
templateUrl: 'CreateGroupContent.html',
controller: CreateGroupCtrl,
size: size,
scope: $scope
});
因此,现在您的函数ok()
和cancel()
将在您的模态视图和模态范围中可用。
看起来您的FeedService
和$modalInstance
混在一起了。它们需要按相同的顺序排列。
相关文章:
- 没有找到相关文章