bindToController with require in Angular Directive

前端 未结 1 730
没有蜡笔的小新
没有蜡笔的小新 2021-01-04 20:22

If my directive uses \"require\" to use a different directive, say ngModel, and uses isolate scope how am I able to use the bindToController<

相关标签:
1条回答
  • 2021-01-04 20:26

    How would you do this without bindToController? All that bindToController: true does is it binds the isolate scope property scope: { prop: "=" } to the property of the controller: this.prop.

    In both cases, the way to pass a "required" controller would be the same, which is to require your own controller and set its property to whatever you want, including other controllers:

    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;
        }
      }
    });
    
    0 讨论(0)
提交回复
热议问题