【问题标题】:How does AngularDart dependency injection work?AngularDart 依赖注入是如何工作的?
【发布时间】:2014-01-09 19:53:50
【问题描述】:

我有一个组件需要从 Dart 代码中访问其根元素。通过在 SO 上阅读Differences between Angular.js and Angular.dart? 并在 AngularDart 源代码中四处寻找,我发现我需要提供一个带有显式类型参数的构造函数。如果其中一个参数的类型为dom.Element,Angular 注入器将为它提供对我的组件根目录的引用。 (在构造函数参数上没有类型会导致异常 NoSuchMethodError : method not found: 'simpleName' 被从 Angular 内部深处抛出。)我的代码现在看起来像这样:

@ng.NgComponent (
    selector: 'math',
    templateUrl: 'packages/embarassing_name_of_my_package/math/math_component.html',
    publishAs: 'ctrl'
)
class MathComponent {
  ng.Scope _scope;
  dom.Element _element;
  ng.NgModel _ngModel;
  ng.NodeAttrs _attrs;

  MathComponent(this._scope, this._element, this._ngModel, this._attrs);

  …
}

ngdom 存在

import 'package:angular/angular.dart' as ng;
import 'dart:html' as dom;

现在是问题。如何最好地发现喷油器反应的那些特殊类型?

此外,我想知道:它是否记录在某处?在哪里?如果不是,那么在 AngularDart 源中,注入器在哪里被配置为这样?

【问题讨论】:

    标签: dependency-injection dart angular-dart


    【解决方案1】:

    类是使用Module.type() 方法(或工厂,值)注册的。

    你可以看看Angular.dart source - search for 'type(' like lib/core/module.dart

    (请忽略type不是函数名的结果)

    当 Angular 调用的构造函数或方法需要参数时,DI 会查找其注册的类型/值/工厂并使用提供的实例(值)或创建新的实例(类型)或使用工厂返回的结果和注入它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      • 2011-04-13
      • 2021-07-02
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      • 2015-11-08
      相关资源
      最近更新 更多