[英]How to call the parent boolean with function to child in angular
我有boolean
是toggleUnit
,我有 function 是onSwitch()
。 我还使用nz-switch
来判断天气,它是Fahrenheit
还是Celcius
list.component.html
<nz-switch [(ngModel)]="toggleUnit" nzCheckedChildren="°F" nzUnCheckedChildren="°C" (ngModelChange)="onSwitchChange()"></nz-switch>
<div class="weather">
<app-weather></app-weather>
</div>
list.component.ts
toggleUnit: boolean = false;
onSwitchChange() {
switch (this.toggleUnit) {
case true: {
this.toggleUnit = true;
}case false: {
this.toggleUnit = false;
}
}
}
天气.component.ts
ngOnInit() {
//console.log(this.toggleUnit);
//trying to get the parent ``toggleUnit`` value, if its true or false
}
您必须将值传递给您的子组件
list.component.html
<nz-switch [(ngModel)]="toggleUnit" nzCheckedChildren="°F" nzUnCheckedChildren="°C" (ngModelChange)="onSwitchChange()"></nz-switch>
<div class="weather">
<app-weather [toggleUnit]="toggleUnit"></app-weather>
</div>
天气.component.ts
@Input() toggleUnit: boolean;
ngOnInit() {
//console.log(this.toggleUnit);
//trying to get the parent ``toggleUnit`` value, if its true or false
}
ngOnChanges(value) {
// called when toggleUnit is updated from parent
console.log(value);
console.log(this.toggleUnit);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.