本文介绍了手动注射达特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何手动注入的角度镖一个实例?这将是等同于以下中angularjs:

  VAR myInjector = angular.injector([NG]);
变量$ HTTP = myInjector.get($ HTTP);


解决方案

从组件内部的code的例子。

@NgComponent(
    选择:评级,
    publishAs:CTRL)
类RatingComponent {
  喷油器_injector;
  RatingConfig _config;  RatingComponent(this._injector){
    _config = _injector.get(RatingConfig);
    // 要么
    _config = injectByName(RatingConfig);
  }  无效injectByName(字符串的typeName){
    _injector.types.takeWhile((E型){
      如果(e.toString()==的typeName){
        _config = _injector.get(E);
        返回false;
      }
      返回true;
    });
  }
}

How do you manually inject an instance in angular dart? This would be the equivalent to the following in angularjs:

var myInjector = angular.injector(["ng"]);
var $http = myInjector.get("$http");
解决方案

A code example from inside a component.

@NgComponent(
    selector: 'rating',
    publishAs: 'ctrl')
class RatingComponent {
  Injector _injector;
  RatingConfig _config;

  RatingComponent(this._injector) {
    _config = _injector.get(RatingConfig);
    // or
    _config = injectByName("RatingConfig");
  }

  void injectByName(String typeName) {
    _injector.types.takeWhile((Type e) {
      if (e.toString() == typeName) {
        _config = _injector.get(e);
        return false;
      }
      return true;
    });
  }
}

这篇关于手动注射达特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 04:36