【发布时间】:2016-10-20 18:30:31
【问题描述】:
有没有办法使用 Angular 的 DI 将依赖项注入装饰器工厂?我们以下面的代码作为一个简化的例子:
@Component({
selector: 'hello-component',
template: '<div>Hello World</div>'
})
export class HelloComponent {
@PersonName()
name: string;
ngAfterViewInit() {
console.log(`Hello, ${this.name}`);
}
}
这里,PersonName 装饰器的预期行为是让它访问 Person 依赖项,并使用它来设置类的 name 属性。
是否有可能为上面的代码实现PersonName 装饰器?
【问题讨论】:
-
你想在
@PersonName()装饰器中设置name的值吗? -
是的——没错。
标签: dependency-injection typescript angular decorator