AngularJS指令可以';找不到所需的控制器
其要点是,在下面的代码中,我需要“modalDirective”来访问要创建的元素列表,它位于“modalController”中。我还需要'modalDirective'来创建一个HTML元素,该元素使用ng模型绑定到'modalController'中的一个作用域变量,因此,如果有其他方法来做这两件事,我完全愿意接受 我使用以下参数打开一个模态:AngularJS指令可以';找不到所需的控制器,angularjs,angularjs-directive,angularjs-controller,Angularjs,Angularjs Directive,Angularjs Controller,其要点是,在下面的代码中,我需要“modalDirective”来访问要创建的元素列表,它位于“modalController”中。我还需要'modalDirective'来创建一个HTML元素,该元素使用ng模型绑定到'modalController'中的一个作用域变量,因此,如果有其他方法来做这两件事,我完全愿意接受 我使用以下参数打开一个模态: var modalOptions = { templateUrl: 'targetUrl/modalPage.html', con
var modalOptions = {
templateUrl: 'targetUrl/modalPage.html',
controller: 'modalController'
}
ModalDialogFactory.openModal(modalOptions);
ModalDialogFactory实际上只是打开了模态:
angular.module('myApp')
.factory('ModalDialogFactory', ['$modal', '$rootScope', function ($modal, $rootScope) {
var modalDialogDefaults = {
backdrop: 'static',
keyboard: false,
scope: $rootScope.$new()
};
var modalInstance;
return {
showModal: function (modalOptions) {
var modalConfig = {};
angular.extend(modalConfig, modalDialogDefaults, modalOptions);
modalInstance = $modal.open(modalConfig);
return modalInstance.result;
},
closeModal: function (selectedObjs) {
if (modalInstance != null) {
modalInstance.close(selectedObjs);
}
}
};
}]);
模态打开,控制器工作正常。到现在为止,一直都还不错。现在,在目标页面中,我想使用一个指令:
angular.module('myApp').directive('modalDirective', function ($q, $http, $compile) {
return {
restrict: 'EAC',
compile: function(element, attr) {
return function(scope, element, attr) {
// Dynamically create new HTML elements
}
};
});
<div modal-directive></div>
然后在modalPage.html中,我引用了modal指令:
angular.module('myApp').directive('modalDirective', function ($q, $http, $compile) {
return {
restrict: 'EAC',
compile: function(element, attr) {
return function(scope, element, attr) {
// Dynamically create new HTML elements
}
};
});
<div modal-directive></div>
但当我尝试要求控制器时,我得到一个错误:
Error: [$compile:ctreq] Controller 'modalController', required by directive 'modalDirective', can't be found!
有人知道我哪里出了问题吗?(假设,使用uibModal或另一个允许解析的$modal)
我知道聚会已经晚了,但在浏览一些旧任务时遇到了这个问题。require的问题是$modal服务将模态条目添加到控制器范围之外的HTML中(除非控制器是完整页面,这似乎不太可能)
您可能希望利用$modal配置的resolve属性,而不是期望传递作用域。所以你会做一些类似的事情:
ModalDialogFactory.showModal({
resolve: {
listOfStuff: function() { return $scope.list; }
}
});
- 创建将属性传递到“解析”属性的模式选项
- 做你喜欢的事
- 通过$modalInstance.close()返回您正在执行的所有更新
ModalDialogFactory.showModal({
resolve: {
listOfStuff: function() { return $scope.list; }
}
});
这将为您提供指令中可用的scope.listofsuff。如何在ModalDialogFactory.openModal()中创建模态?你能提供更多的代码吗?@Sharko我在编辑中包含了工厂,所以你现在应该在上面的问题中看到它。