1

GitHub 上的 angular.dart/lib/directive/module.dart文件 有很多行,例如

// class NgDirectiveModule extends Module {
//  NgDirectiveModule() {

    value(NgADirective, null); // <--

这个声明的目的是什么。
第二个参数记录为The [value] is what actually will be injected. 为什么我要null被注入?

4

1 回答 1

1

您想要一个null,因为根注入器中不存在该指令。如果没有这些语句,尝试注入不存在的指令会导致程序因“未知类型”注入器错误而崩溃。

当 Angular 遍历 DOM 创建指令时,它们在 DOM 遍历期间创建的子注入器中可用。例如

<div ng-model="foo" my-directive>...</div>

在 MyDirective 指令中,您可以注入任何其他指令:

class MyDirective {
  MyDirective(NgModel model) {
    if (model.viewValue == "party") dance();
  }
}

您可以对任何指令执行此操作,例如ng-click, ng-class,但是大多数指令没有有用的公共接口。但是,该null值很有用:

class MyDirective {
  MyDirective(NgRepeatDirective repeat) {
    if (repeat != null) {
       // this element is being repeated
    } else {
       // this element is not being repeated.
    }
  }
} 
于 2014-01-30T19:43:43.133 回答