假设我们有这个代码
父模板:
<wrapper>
<custom-element [foo]="true">
</wrapper>
子模板(选择器:'包装器')
<ng-content>
子代码:
ngAfterContentInit(): void {
this.customelementRef.foo= false;
}
正如预期的那样,foo的值是false。
我想在ngAfterContentInit中将默认值设置为'false',除非父级覆盖它。如何更改此代码以使父绑定优先?
使用默认值:
@Component(...)
export class ChildComponent {
@Input() foo: boolean = false;
}
使用@Input()装饰器的说明
<custom-element [foo]="truthValue"> // Change the truthValue variable, and the child will take the changes.