Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript @主机()不带<;天然气含量>;在Angular2不工作_Javascript_Angular_Angular Decorator_Angular2 Ngcontent - Fatal编程技术网

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内容)

问题:

  • 为什么第一个代码不工作(没有ng内容)
  • @主机只能与ng内容一起工作
  • 在这两种情况下,由于编译HTML的结构相同,ng内容的区别是什么
  • 以下是Plunker供参考:


    看起来您需要使用
    查看提供程序
    而不是
    提供程序
    来处理
    @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;
          }
        }