繁体   English   中英

如何在 angular 中使用 function 调用父 boolean 到子级

[英]How to call the parent boolean with function to child in angular

我有booleantoggleUnit ,我有 function 是onSwitch() 我还使用nz-switch来判断天气,它是Fahrenheit还是Celcius

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;
     }
  }
}

天气.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>

天气.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM