这个问题的解决方案是使用Angular的ChangeDetectorRef,这会强制Angular重新检查组件绑定并进行必要的更改。可以在下拉列表中添加ChangeDetectorRef依赖项,然后在更改下拉列表选项时调用它。代码示例如下:
在组件中添加依赖项:
import { Component, ChangeDetectorRef } from '@angular/core';
@Component({
selector: 'my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponent {
constructor(private cdr: ChangeDetectorRef) { }
...
}
在更改选项时调用ChangeDetectorRef:
this.myOptions = [ /* 新的选项 */ ];
this.cdr.detectChanges();
下一篇:Angular条件验证