我有一个下拉菜单,我想动态地禁用它,我正在使用@Input从父组件获取值,并在子组件中禁用它。
@input值已正确返回并在ngOnchange上触发,我已经控制了@input值,并按预期返回。
但是,当我在HTML上打印值时,该值不会使用onChange更新。 (值在组件中更改,模板未更新)
我已经尝试过this.ref.detectChanges()和ngZone仍然不起作用
HTML
<div ngbDropdown #commonDropdown="ngbDropdown" class="d-inline-block" style="width: 100%;" [autoClose]="true">
<button class="btn btn-primary truncate" id="dropdownBasic1" ngbDropdownToggle [disabled]="disableChannel">
{{values.selected.value}}
</button>
<div ngbDropdownMenu aria-labelledby="dropdownBasic1" style="width: 100%;">
<button *ngFor="let value of values.data" class="dropdown-item" (click)="selectDropDown(value); commonDropdown.close();">{{value.value}}
</button>
</div>
</div>
组件:
export class CommonComponent implements OnInit, OnChanges {
@Input()
disableChannel: boolean;
@Input()
values: DropDownObject;
@Output()
valueSelected: EventEmitter < any > = new EventEmitter();
private selectedValues: DropDownObject;
constructor(private ref: ChangeDetectorRef, private ngZone: NgZone) {
}
ngOnInit() {
this.emitSelectedValue(this.values);
}
ngOnChanges(simpleChanges: SimpleChanges) {
console.log(this.disableChannel);
// boolean returns as expected however, binding in the view doesn't work. Button is not disabled when the value returns true
}
selectDropDown(selectedChannel): void {
this.values.setSelected(selectedChannel);
this.selectedValues = new DropDownObject();
this.selectedValues.setSelected(selectedChannel);
this.selectedValues.setData(this.values.getData());
this.emitSelectedValue(this.selectedValues);
}
emitSelectedValue(data) {
this.valueSelected.emit(data);
}
}