[英]Unable to pass value from parent to child component in angular 9
Basically scenario is, i am shifting the value of variable up to its grand-parent component using EventEmitter (till now its working fine).基本上情况是,我正在使用 EventEmitter 将变量的值转移到其祖父组件(直到现在它工作正常)。
but once value reaches at grand-parent i want to pass that value to its another child component.但是一旦值到达祖父级,我想将该值传递给它的另一个子组件。 i am doing it with
@input name:string
at child component.我在子组件中使用
@input name:string
来做这件事。 but don't know why i am not able to get that name in that child component.但不知道为什么我无法在该子组件中获得该名称。
parent =>父母 =>
//html code
<personal-section (childName)="getName($event)"></personal-section>
<profile-section [name]="name"></name>
//inside profile.ts
getName($event) {
console.log(this.name); // <--- upto this console, program works fine
this.name= $event;
}
child =>孩子 =>
@Input() name: any;
ngOnChanges() {
if (this.name) {
console.log('users name is ', this.name); //<--this never prints.
}
}
but whenever i perform click event in grand-child's component, value perfectly reaches upto its grand-parent.但是每当我在孙子组件中执行单击事件时,值都会完美地到达其祖父级。 but after that i am not able to get it in its another child ie final destination ;)
但在那之后,我无法在另一个孩子身上得到它,即最终目的地;)
Thanks in advance.提前致谢。
Replacing the getName($event)
in the controller with getName(event)
is only a matter of convention.更换
getName($event)
与控制器getName(event)
只是习惯问题。
Besides, if you only want to send the name
variable from one child to another child, you could do it without involving the parent using custom variable.此外,如果您只想将
name
变量从一个孩子发送到另一个孩子,您可以使用自定义变量在不涉及父级的情况下完成。 Try the following尝试以下
Parent template父模板
<personal-section (childName)="profileSection1.setName($event)"></personal-section>
<profile-section #profileSection1></name>
The @Input
decorator can then be removed from the name
property in 'profile section' component.然后可以从“配置文件部分”组件的
name
属性中删除@Input
装饰器。
Profile section component型材截面组件
export class ProfileSectionComponent implements OnInit {
name: any;
public setName(name: any) {
this.name = name;
console.log('User name is ', this.name);
}
.
.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.