javascript - 更新 mat-select defaultValue 不起作用
问题描述
我有一个视图(表),其中有一个操作,当我单击该操作时,我会传递表中该行的值以更新使用它打开的模式的 defaultValue。我在模式中有下拉菜单,我正确获得了 defaultSelection 的 Id,甚至下拉组件也会相应地更新。
发生的事情是 defaultValue 保持不变,当我选择不同的行时它不会更新:
这是我输入的地方:
<app-ui-input-select
(selectionChanged)="onCreateSelectionChange($event, 'typeId')"
[options]="attributes"
[defaultValue]="duplicatedOriginValueRow.type"
class="master-data-modal-attributes-filter"
[label]="'Type*'"
[tooltip]="'Type*'">
每次我用与 id 对应的新类型更新duplicatedOriginValueRow对象时,它不会在内部更新,我没有传递对象的相同引用,只是为了让你们现在。
这是我的组件输入选择 HTML:
<mat-form-field appearance="outline" floatLabel="always" [matTooltip]="tooltip">
<mat-label *ngIf="label">{{ label }}</mat-label>
<mat-select
[(ngModel)]="defaultValue.value"
(selectionChange)="selectionChanged.emit($event)"
[formControl]="selectControl"
name="name"
disableOptionCentering
>
<mat-option *ngFor="let option of options" [value]="option.value">
{{
isTitleString(option.title) ? (option.title | uppercase) : option.title
}}
</mat-option>
</mat-select>
</mat-form-field>
我的 TS 文件:
import {
Component,
OnInit,
ChangeDetectionStrategy,
Input,
forwardRef,
OnDestroy,
Output,
EventEmitter,
SimpleChanges,
OnChanges,
} from '@angular/core';
import {
ControlValueAccessor,
FormControl,
NG_VALUE_ACCESSOR,
} from '@angular/forms';
import { Subscription } from 'rxjs';
@Component({
selector: 'app-ui-input-select',
templateUrl: './ui-input-select.component.html',
styleUrls: ['./ui-input-select.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [
{
provide: NG_VALUE_ACCESSOR,
multi: true,
useExisting: forwardRef(() => UiInputSelectComponent),
},
],
})
export class UiInputSelectComponent
implements OnInit, OnDestroy, ControlValueAccessor, OnChanges {
@Input() name?: string;
@Input() label?: string;
@Input() tooltip?: string;
@Input() options: { title: string; value: any }[] = [];
@Input() defaultValue = { title: '', value: '' };
_defaultValue = {title: '', value: ''}
@Output() emitDefinedColumnValue = new EventEmitter<string>();
@Output() selectionChanged = new EventEmitter<string>();
readonly selectControl: FormControl = new FormControl(null);
private readonly subscriptions: Subscription[] = [];
private readonly changeListeners: ((value) => void)[] = [];
private readonly touchedListeners: (() => void)[] = [];
ngOnInit(): void {
const sub = this.selectControl.valueChanges.subscribe((value) =>
this.notifyChange(value)
);
this.subscriptions.push(sub);
}
ngOnChanges(changes: SimpleChanges) {
if(changes.defaultValue.currentValue) {
this._defaultValue = changes.defaultValue.currentValue
}
}
compareById(f1: any, f2: any): boolean {
return f1 && f2 && f1.value === f2.value;
}
ngOnDestroy(): void {
this.subscriptions.forEach((s) => s.unsubscribe());
}
registerOnChange(fn: any): void {
this.changeListeners.push(fn);
}
registerOnTouched(fn: any): void {
this.touchedListeners.push(fn);
}
setDisabledState(isDisabled: boolean): void {
if (isDisabled) {
this.selectControl.disable();
} else {
this.selectControl.enable();
}
}
writeValue(obj: any): void {
this.selectControl.setValue(obj, { emitEvent: false });
}
notifyTouched(): void {
for (const listener of this.touchedListeners) {
listener();
}
}
isTitleString(title: string): boolean {
return typeof title === 'string';
}
notifyChange(value: any): void {
this.notifyTouched();
for (const listener of this.changeListeners) {
listener(value);
}
}
}
我还尝试检查 ngOnChanges 生命周期钩子中的内容,并且在那里我得到了正确的值(更改后的值),似乎下拉列表没有更新 html。
我感谢任何帮助。
非常感谢!
解决方案
推荐阅读
- android - 点击通知后如何打开特定屏幕?
- oracle - 最短旅行时间
- android - 对话框,设置阴影
- azure - Azure Blob 触发器有时需要太多时间才能触发
- css - 如何设置绝对元素高度 100% og parent
- vector - 是否可以在 protobuf 消息中包含向量字段以生成 Rust 结构?
- java - 如何根据主题动态应用计划的 kafka 消费者?
- reactjs - 在 react-native 中从 StackNavigator 返回组件的问题。得到一个空白屏幕,但 cosole.log 没问题
- tpu - 珊瑚开发板:计算机/树莓派无法识别
- hibernate - 关于 OneToOne 关系的休眠 N+1 问题