Javascript @主机()不带<;天然气含量>;在Angular2不工作
我试图通过使用Javascript @主机()不带<;天然气含量>;在Angular2不工作,javascript,angular,angular-decorator,angular2-ngcontent,Javascript,Angular,Angular Decorator,Angular2 Ngcontent,我试图通过使用@host将依赖项的搜索限制为仅搜索其主机 但如果没有ng内容或转换,它就无法工作 以下代码不工作(没有ng内容) 问题: 为什么第一个代码不工作(没有ng内容) @主机只能与ng内容一起工作 在这两种情况下,由于编译HTML的结构相同,ng内容的区别是什么 以下是Plunker供参考: 看起来您需要使用查看提供程序而不是提供程序来处理@Host,如中所示 什么是“不工作”的确切含义?提供程序:[{provide:TestService,useClass:TestService}]
@host
将依赖项的搜索限制为仅搜索其主机
但如果没有ng内容
或转换,它就无法工作
以下代码不工作(没有ng内容)
问题:
看起来您需要使用
查看提供程序
而不是提供程序
来处理@Host
,如中所示
什么是“不工作”的确切含义?
提供程序:[{provide:TestService,useClass:TestService}]
与提供程序:[TestService]
相同,在第一个示例中,在没有内容投影的情况下使用@host时,无法获取TestService的引用……尽管孩子有一个主机,但请提供一个我不知道的插件。我自己从来没有用过@Host()
(或者是很久以前的事了,我都不记得了)
// Root Component
@Component({
selector: 'my-app',
template: '<parent></parent>'
})
export class RootComponent { }
@Component({
selector: 'parent',
template: '<child></child>',
providers:[{provide:TestService,useClass:TestService}]
})
export class ParentComponent {
name: string = '';
constructor(abc:TestService){
abc.name='SomeName';
this.name=abc.name
}
}
@Component({
selector: 'child',
template: '<h1>{{parent}}</h1>'
})
export class ChildComponent {
parent: string = ""
constructor(@Host() testService: TestService) {
this.parent= 'My parent is :'+testService.name;
}
}
@Component({
selector: 'my-app',
template: '<parent><child></child></parent>'
})
export class RootComponent { }
@Component({
selector: 'parent',
template: '<ng-content></ng-content>',
providers:[{provide:TestService,useClass:TestService}]
})
export class ParentComponent {
name: string = '';
constructor(abc:TestService){
abc.name='SomeName';
this.name=abc.name
}
}
@Component({
selector: 'child',
template: '<h1>{{parent}}</h1>'
})
export class ChildComponent {
parent: string = ""
constructor(@Host() testService: TestService) {
this.parent= 'My parent is :'+testService.name;
}
}