如果我的指令使用“require
”来使用其他指令,请说ngModel
,并使用隔离范围,我如何能够使用bindToController
语法并且仍然能够从 Controller 访问可注入(inject)物(ngModelController
)?
最佳答案
没有bindToController
,您将如何做到这一点? bindToController: true
所做的全部工作就是将隔离范围属性scope: { prop: "=" }
绑定(bind)到 Controller 的属性this.prop
。
在这两种情况下,传递“必需” Controller 的方式都是相同的,即对自己的 Controller require
并将其属性设置为所需的任何值,包括其他 Controller :
app.directive("foo", function(){
return {
require: ["foo", "bar"],
controller: function(){
this.doSomethingWithBar = function(){
this.bar.doSomething();
};
},
controllerAs: "ctrl",
bindToController: true,
link: function(scope, element, attrs, ctrls){
var foo = ctrls[0], bar = ctrls[1];
foo.bar = bar;
}
}
});