注入的angular.ui模式内的角控制器的依赖正确的方法 [英] The correct way to inject an angular controller dependency inside an angular.ui modal
问题描述
以下angular.ui模态例如显示了 modalInstance
调用 ModalIntanceCtrl
这是后来作为一个函数创建:
following angular.ui Modal example shows the modalInstance
calling a ModalIntanceCtrl
which is later created as a function:
var ModalDemoCtrl = function ($scope, $modal, $log) {
$scope.items = ['item1', 'item2', 'item3'];
$scope.open = function () {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
resolve: {
items: function () {
return $scope.items;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
};
var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
$scope.items = items;
$scope.selected = {
item: $scope.items[0]
};
$scope.ok = function () {
$modalInstance.close($scope.selected.item);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
};
我有2个问题/问题:
I have a 2 questions/problems:
-
的文档建议以另一种方式,例如创建一个控制器(由于微小的问题):
the docs recommend creating a controller in another way (due to minification issues) for example:
myApp.controller('GreetingCtrl',['$范围',函数($范围){
$ scope.greeting ='霍拉!;
}]);
但是,如果我创建这样的控制器,我怎么可能把它注射到modalInstance?
But if I create the controller like this, how could I Inject it into the modalInstance?
- 我打电话这里的控制器不是一个模式实例控制器,但我的世界
loginCtrl
,这是一个问题?我要继承不知何故loginCtrl或从ModalInstanceCtrl打电话吗?如果是的话 - 如何准确
- The controller I call here isn't a Modal Instance controller but my global
loginCtrl
, is this a problem? should I subclass somehow the loginCtrl or call it from the ModalInstanceCtrl? and if so - how exactly?
我会很高兴有关此指导和clarfication。
谢谢!
I'll be glad for guidance and clarfication about this. Thanks!
推荐答案
您的问题不是很清楚,但如果你使用模块API声明控制器,那么你可以提供控制器模态服务作为一个字符串
You question is not very clear, but if you declare controller using the module API, then you can provide the controller to the modal service as a string
myApp.controller('ModalInstanceCtrl', ['$scope', function($scope) { $scope.greeting = 'Hola!'; }]);
控制器:'ModalInstanceCtrl,
同样可以为 loginCtrl
做,如果你想使用的模式服务。
The same can be done for loginCtrl
if you want to use that in the modal service.
这篇关于注入的angular.ui模式内的角控制器的依赖正确的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!