gpt4 book ai didi

javascript - 如何在 Angular 中使用子函数调用父 bool 值

转载 作者:行者123 更新时间:2023-12-02 22:45:46 24 4
gpt4 key购买 nike

我有 boolean,它是 toggleUnit,我有一个函数,它是 onSwitch()。我还使用 nz-switch 来表示天气,它是 FahrenheitCelcius

list.component.html

<nz-switch [(ngModel)]="toggleUnit" nzCheckedChildren="&deg;F" nzUnCheckedChildren="&deg;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;
}
}
}

weather.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="&deg;F" nzUnCheckedChildren="&deg;C" (ngModelChange)="onSwitchChange()"></nz-switch>

<div class="weather">
<app-weather [toggleUnit]="toggleUnit"></app-weather>
</div>

weather.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);
}

关于javascript - 如何在 Angular 中使用子函数调用父 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58405324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com