本文介绍了bindToController有需要角度指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的指令使用要求使用不同的指令,说 ngModel ,并采用隔离范围如何我能够使用 bindToController 语法和仍然能够从控制器访问注射剂( ngModelController

解决方案

你会如何做没有 bindToController ?所有这一切 bindToController:真正的做的是它结合了分离scope属性范围:{道具:=} 来的控制器的特性: this.prop

在这两种情况下,通过一个必需的控制器的方式是一样的,这是要求您自己的控制器,并设置为任何你想要的属性,包括其他的控制器:

  app.directive(富,函数(){
  返回{
    要求:[富,酒吧],
    控制器:功能(){
      this.doSomethingWithBar =功能(){
        this.bar.doSomething();
      };
    },
    controllerAs:CTRL
    bindToController:真实,
    链接:功能(范围,元素,ATTRS,ctrls监视){
      无功富= ctrls监视[0],酒吧= ctrls监视[1];
      foo.bar =栏;
    }
  }
});

If my directive uses "require" to use a different directive, say ngModel, and uses isolate scope how am I able to use the bindToController syntax and still be able to access the injectables (ngModelController) from the controller?

解决方案

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;
    }
  }
});

这篇关于bindToController有需要角度指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-06 23:23