您可以使用name
和controller="@"
选项。
.directive('test', [function($popover) {
return {
restrict: 'E',
scope: false,
controller : "@",
name:"controller",
template:'<div>{{value}}</div>',
link: function(scope, elem, attrs) {
}
};
}
例如: -
<test controller="ctrl1"></test>
.....
<test controller="ctrl2"></test>
当您指定controller= '@'
角将寻找注册的控制与元素的属性指定的属性值(你可以给属性的任何名称)中所提到的name
选项。
Plnkr
下面是从角指令实施片段: -
if (controllerDirectives) {
....
controller = directive.controller;
if (controller == '@') {
controller = attrs[directive.name];
}
controllerInstance = $controller(controller, locals);
来源
2014-08-28 19:40:52
PSL
我想你不能注入两个控制器,你应该考虑其他的方法 – 2014-08-28 19:32:49
你不必注入控制器。您可以使用指定为属性的控制器。那种你在找什么? – PSL 2014-08-28 19:48:53