需要使用Angular的变更检测机制手动更新UI。修改组件中的方法,使其在数据发生更改时触发变更检测,并使用Angular提供的ChangeDetectorRef服务来手动触发变更检测。
示例代码:
import { Component, ChangeDetectorRef } from '@angular/core';
@Component({
selector: 'app-example',
template: {{ data }}
})
export class ExampleComponent {
data: string;
constructor(private cd: ChangeDetectorRef) { }
updateData() { this.data = 'New Data'; this.cd.detectChanges(); } }
在上述示例代码中,updateData()方法中的this.cd.detectChanges()手动触发了变更检测,使其能够更新UI。