Angular:如何控制ng-content中的输入绑定优先级?

问题描述 投票:0回答:1

假设我们有这个代码

父模板:

<wrapper>
 <custom-element [foo]="true">
</wrapper>

子模板(选择器:'包装器')

<ng-content>

子代码:

ngAfterContentInit(): void {
    this.customelementRef.foo= false;
  }

正如预期的那样,foo的值是false。

我想在ngAfterContentInit中将默认值设置为'false',除非父级覆盖它。如何更改此代码以使父绑定优先?

javascript angular
1个回答
0
投票

使用默认值:

@Component(...)
export class ChildComponent {
  @Input() foo: boolean = false;
}

使用@Input()装饰器的说明

  1. 除非父级更改,否则该值将保持为false。
  2. 只要父母想要改变价值,他们所要做的就是 <custom-element [foo]="truthValue"> // Change the truthValue variable, and the child will take the changes.
© www.soinside.com 2019 - 2024. All rights reserved.