angular - 将数据从父母传递给孩子不能有条件地工作 - Angular 7/8
问题描述
我正在开发一个 Angular 7/8 应用程序。我创建了一个自定义Select
元素作为组件。无论我在哪里集成组件,我都会向它传递两件事。
1 -items
选择元素应显示为选项的数组。
2 - 显示名称 ieSelect an Option
等的占位符。
我正在使用@Input()
并@Output()
在孩子和父母之间进行互动,它对我来说非常有效。
我正在做的是,我<app-select></app-select>
在父元素中显示了一个元素,而另一个元素是根据用户从第一个中选择一个选项时显示的<app-select></app-select>
。例如,在第一个select
选项中Monthly and Yearly
,当用户选择时,Monthly
将一种类型的数据加载到第二个select
元素中,否则,在每年的选项中,会显示另一种类型的数据,这里就会出现问题。
Whenever any of the options Monthly or Yearly
is selected, the items
are shown and the placeholder is set, but when I try to choose the second option, the placeholder changes but the data doesn't get binded to the view, and the items
from first selection is shown. 我不知道是什么问题。
选择select
器
<div class="col-sm-12 col-md-6 col-lg-4 cp-sec">
<app-select [items]='paymentBasisItems' [ph]="pbPh" (selectedItem)='paymentBasisSelected($event)'></app-select>
</div>
<div class="col-sm-12 col-md-6 col-lg-4 cp-sec" *ngIf="isPaymentBasisSelected">
<app-select [items]='paymentTimeItems' [ph]="ptPh" (selectedItem)='paymentTimeSelected($event)'></app-select>
</div>
变量
public paymentBasisItems: Array<any>;
public pbPh: string;
public paymentTimeItems: Array<any>;
public ptPh: string;
初始化第一个Select
ngOnInit() {
this.pbPh = 'Select Payment Basis';
this.paymentBasisItems = ['Monthly', 'Yearly'];
}
从第一个选择选项时Select
paymentBasisSelected(event: any) {
if (event === 'Monthly') {
this.isPaymentBasisSelected = true;
this.ptPh = 'Select Number Of Months';
this.paymentTimeItems = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
} else if (event === 'Yearly') {
this.isPaymentBasisSelected = true;
this.ptPh = 'Select Number Of Years';
this.paymentTimeItems = ['select', 'a', 'year', 'nadeem', 'khan'];
} else {
this.isPaymentBasisSelected = false;
}
}
组件代码select
//Template
<div class="an-select">
<div class="ans-head waves-effect waves-dark" (click)="toggleOpenSelect($event)" data-an-selected>{{ph}}</div>
<div class="ans-opts">
<div class="option waves-effect waves-light" *ngFor="let item of selectItems" (click)="optionSelected(item)">{{item}}</div>
</div>
</div>
//Code
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-select',
templateUrl: './select.component.html',
styleUrls: ['./select.component.scss']
})
export class SelectComponent implements OnInit {
constructor() { }
@Input() items;
@Input() ph;
@Output() selectedItem = new EventEmitter();
public selectItems: Array<any>;
public selectPh: string;
public currEle;
public currEleShown = false;
toggleOpenSelect(event) {
this.currEle = event.target;
if (!this.currEleShown) {
(this.currEle.parentNode as any).querySelector('.ans-opts').style.display = 'block';
this.currEleShown = true;
} else if (this.currEleShown) {
(this.currEle.parentNode as any).querySelector('.ans-opts').style.display = 'none';
this.currEleShown = false;
}
}
optionSelected(item) {
this.ph = item;
this.selectedItem.emit(item);
(this.currEle.parentNode as any).querySelector('.ans-opts').style.display = 'none';
this.currEleShown = false;
}
ngOnInit() {
this.selectItems = this.items;
this.selectPh = this.ph;
}
}
解决方案
您必须添加另一个组件生命周期方法:ngOnChanges,您基本上可以在其中执行与 ngOnInit 相同的操作。
或者,去掉“selectItems”和“selectPh”,直接使用“items”和“ph”(也在模板中)
推荐阅读
- html - 如何从css加载svg图片
- c# - 在 C# 中读取 C++ ofstream 写入的文件
- javascript - Json.parse 是空的,即使在 if 语句中说只在它不为空时运行?
- python - 使用 django-postman 的用户到用户消息传递
- python - 从数据框列中的列表中查找元素
- uwp - UWP:DataGrid、MenuFlyout 右键单击
- git - 删除“旧”项目的断开连接的 Git 历史记录
- unit-testing - 在测试 api 控制器时模拟存储库
- android - onCaptureProgressed 与 onCaptureCompleted,Android 中的 Camera2
- wpf - 在 ScrollViewer 内自动滚动网格导致闪烁/脉冲效果