parent component class
export class Parent {
show: boolean = false;
constructor() { }
showChild() {
this.show = true;
}
}
parent component template
<child [isShow]="show"></child>
child component class
export class Child {
@Input isShow: boolean = false;
constructor() { }
onClick() {
this.isShow = false;
}
}
after I triggered onClick() in child component, the showChild() would not work to show the child component.
why?
We can get child component values in the parent component by creating a reference to the child component using the @ref directive in the Parent component. Using the reference instance, you can access the child component values in the parent.
The value is being passed exclusively from parent to child because you're using square brackets.
In order for the value to go both ways, you need to use a two-way data binding.
That means your isShow attribute should be like:
@Input() isShow: boolean;
@Output() isShowChange = new EventEmitter<boolean>();
And the template should be
<child [(isShow)]="show"></child>
or
<child [isShow]="show" (isShowChange)="show = $event"></child>
Take a look at the two-way data binding tutorial page:
https://angular.io/guide/template-syntax#two-way-binding---
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With