在Angular中,如果mat-select的双向绑定不起作用,并且通过代码分配值会导致UI重置,可以尝试以下解决方法:
{{option}}
import { Component, ChangeDetectorRef } from '@angular/core';
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent {
selectedValue: any;
options: any[];
constructor(private changeDetectorRef: ChangeDetectorRef) {}
assignValue() {
this.selectedValue = 'some value';
this.changeDetectorRef.detectChanges();
}
}
在上面的例子中,通过调用changeDetectorRef.detectChanges()
手动触发变更检测,以确保UI正确更新。
希望这些解决方法能帮助你解决mat-select双向绑定不起作用的问题。