1

GitHubの angular.dart/lib/directive/module.dart ファイルに は、次のような行がたくさんあります。

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

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

この声明の目的は何ですか。
2 番目のパラメーターは、「なぜ注入し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-clickng-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 に答える