[英]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.