bindToController中带有require的bindToController

sil*_*zir 9 angularjs angularjs-directive angularjs-scope

如果我的指令使用" require"来使用不同的指令,比如说ngModel,并使用隔离范围我怎样才能使用bindToController语法并仍然能够ngModelController从控制器访问injectables()?

New*_*Dev 21

你怎么会这样做bindToController?所有这一切bindToController: true都是将isolate scope属性绑定scope: { prop: "=" }到控制器的属性:this.prop.

在这两种情况下,传递"必需"控制器的方式都是相同的,这是require您自己的控制器并将其属性设置为您想要的任何内容,包括其他控制器:

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;
    }
  }
});
Run Code Online (Sandbox Code Playgroud)