AngularDart 依赖注入是如何工作的? [英] How does AngularDart dependency injection work?

查看:28
本文介绍了AngularDart 依赖注入是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an component that needs to access its root element from the Dart code. By reading Differences between Angular.js and Angular.dart? here on SO and grepping around in the AngularDart source code I figured out that what I need is to provide a constructor with explicitly typed arguments. If one of the arguments has type dom.Element, it will be given the reference to my components root by the Angular injector. (Failing to have types on the constructor arguments results in an exception NoSuchMethodError : method not found: 'simpleName' being thrown from deep down in Angular internals.) My code now looks like this:

@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;

现在是问题.我如何最好地发现注入器反应的那些特殊类型?

Now the question. How do I best discover those special types that the injector reacts to?

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

Additionally, I'd like to know: Is it documented somewhere? Where? If not, where in the AngularDart source is the injector being configured to behave like this?

推荐答案

使用 Module.type() 方法(或工厂、值)注册类.

Classes are registered using the Module.type() method (or factory, value).

你可以看看Angular.dart 源 - 搜索 'type('像 lib/core/module.dart

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

(Please ignore the results where type is not the function name)

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

When a constructor or a method called by Angular needs a parameter DI looks up its registered types/values/factories and uses a provided instance (value) or creates a new instance (type) or uses the result that a factory returns and injects it.

这篇关于AngularDart 依赖注入是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆