如何在AngularJS Injector中解析和创建泛型类

时间:2016-01-21 08:15:09

标签: dependency-injection typescript angular

有一个类如下,

export class SiteEventServiceType<T> {
}

我如何使用angularjs 2 Injector为上述类创建实例。

试过但不行:

 Injector.resolveAndCreate([SiteEventServiceType<string>]).get(SiteEventServiceType<string>);

1 个答案:

答案 0 :(得分:0)

这个例子在https://github.com/angular/angular/pull/1779

发布了很长一段时间
@Component(
  selector: 'my-component',
  // now providers:
  injectables: const [
      const Binding(
          const TypeLiteral<Stream<String>>, 
          toFactory: MyComponent.streamFactory)
  ]
)
@View(
  directives: const [MyChildComponent],
  template: '<my-child-component></my-child-component>'
)
class MyComponent {
  static Stream<String> streamFactory() => new Stream.fromIterable(['Hello']);
}

@Component(
  selector: 'my-child-component'
)
@View(
  template: '{{lastValue}}'
)
class MyChildComponent {
  String lastValue;

  MyChildComponent(Stream<String> stringStream) {
    stringStream.listen((value) => lastValue = value);
  }
}

另见