Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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
Html 祖辈中定义的子模块和父模块之间的角度传递变量_Html_Angular_Typescript_Inheritance - Fatal编程技术网

Html 祖辈中定义的子模块和父模块之间的角度传递变量

Html 祖辈中定义的子模块和父模块之间的角度传递变量,html,angular,typescript,inheritance,Html,Angular,Typescript,Inheritance,假设我有一个angular中的祖父母类,它有一个html,如下所示: <parent> <child></child> </parent> @Component({ selector: 'parent', template: ` <div> My name is {{name}} <br> children: <ng-content></n

假设我有一个angular中的祖父母类,它有一个html,如下所示:

<parent>
    <child></child>
</parent>
@Component({
  selector: 'parent',
  template: `
    <div>
      My name is {{name}} <br>
      children: 
        <ng-content></ng-content>
    </div>
  `,
  styleUrls: ['./parent.component.css']
})
export class ParentComponent {
  name = 'Donald Duck';
}

@Component({
  selector: 'child',
  template: `
    <div>
      My parent is called {{parentName}}
    </div>
  `,
  styleUrls: ['./child.component.css']
})
export class ChildComponent {
  @Input() parentName: string;
}


父对象有一个子对象需要呈现的变量,父对象将该变量传递给子对象,子对象将其绑定到自己预定义的html中。我怎样才能做到这一点?ViewChild和ContentChild似乎都不能完全工作。

首先,使用@ViewChild引用子组件,然后使用AfterViewInit;要传递变量-您需要等待视图初始化,然后才能访问@ViewChild

首先,使用@ViewChild引用子组件,然后使用AfterViewInit;要传递变量-您需要等待视图初始化,然后才能访问@ViewChild

假设父组件和子组件如下所示:

<parent>
    <child></child>
</parent>
@Component({
  selector: 'parent',
  template: `
    <div>
      My name is {{name}} <br>
      children: 
        <ng-content></ng-content>
    </div>
  `,
  styleUrls: ['./parent.component.css']
})
export class ParentComponent {
  name = 'Donald Duck';
}

@Component({
  selector: 'child',
  template: `
    <div>
      My parent is called {{parentName}}
    </div>
  `,
  styleUrls: ['./child.component.css']
})
export class ChildComponent {
  @Input() parentName: string;
}
然后,祖父母模板将非常简单:

<parent>
  <child></child>
</parent>


演示:

假设父组件和子组件如下所示:

<parent>
    <child></child>
</parent>
@Component({
  selector: 'parent',
  template: `
    <div>
      My name is {{name}} <br>
      children: 
        <ng-content></ng-content>
    </div>
  `,
  styleUrls: ['./parent.component.css']
})
export class ParentComponent {
  name = 'Donald Duck';
}

@Component({
  selector: 'child',
  template: `
    <div>
      My parent is called {{parentName}}
    </div>
  `,
  styleUrls: ['./child.component.css']
})
export class ChildComponent {
  @Input() parentName: string;
}
然后,祖父母模板将非常简单:

<parent>
  <child></child>
</parent>


演示:

这非常接近,但有没有办法让我在父母或孩子身上完成这项任务,让祖父母尽可能简单?谢谢,这太完美了!这很接近,但是有没有一种方法可以让我在父母或孩子身上完成这项任务,让祖父母尽可能简单?谢谢,这太完美了!这可以与@Wilhelm的答案结合使用以允许父级或子级自己获取这些属性吗?这可以与@Wilhelm的答案结合使用以允许父级或子级自己获取这些属性吗