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

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

问题描述

我有一个需要从Dart代码访问其根元素的组件.通过阅读 Angular.js和Angular.dart之间的区别?因此,我在AngularDart源代码中四处摸索,发现我需要为构造函数提供显式类型的参数.如果其中一个参数的类型为dom.Element,则Angular注入器将为我的组件根提供引用. (如果没有在构造函数参数上使用类型,则会导致从NoSuchMethodError : method not found: 'simpleName'.)我的代码现在看起来像这样:

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天全站免登陆