angular - ngAfterViewChecked 还不够晚
问题描述
我正在使用物化作为引导程序的替代品。
我有一个问题,其中尚未加载选择的选项,在这种情况下调用物化的初始化将无法正确显示:
给定我的数据:
@Component({
selector: 'app-form',
templateUrl: './form.component.html',
styleUrls: ['./form.component.sass']
})
export class FormComponent implements OnInit {
inputTest= {
value: 1,
choiceValues: [1,2,3,4]
};
}
以下html:
<select [(ngModel)]='inputTest.value' [ngModelOptions]="{standalone: true}">
<ng-container *ngFor="let choiceValue of inputTest.choiceValues">
<option [ngValue]="choiceValue">{{choiceValue}}</option>
</ng-container>
</select>
具有以下内容:
ngAfterViewChecked() {
if (this.needsInitialization) {//just a guard to have the initialization once
this.needsInitialization = false;
let elems: NodeListOf<Element> = this.elementRef.nativeElement.querySelectorAll('select');
M.FormSelect.init(elems, {});
}
将显示(奇怪的是,所有选项都是可见的!):
并点击:
使用 setTimeout 将起作用:
ngAfterViewChecked() {
if (this.needsInitialization) {
this.needsInitialization = false;
setTimeout(() => {
let elems: NodeListOf<Element> = this.elementRef.nativeElement.querySelectorAll('select');
M.FormSelect.init(elems, {});
}, 1000);
}
根据:https: //angular.io/guide/lifecycle-hooks#lifecycle-event-sequence ngAfterViewChecked是ngDestroy之前的最后一个序列。但它并没有做到它所承诺的:完全加载了视图。
当 Angular 的生命周期无法正常工作时,如何使用比超时更好的方法?
解决方案
推荐阅读
- netsuite - 如何在子列表子记录中设置值?(SuiteScript 1.0)
- python - 如何正确关闭使用 local_addr 的异步连接
- javascript - 如何将 RRule 应用于拖到日历的外部事件
- powershell - 使用带有哈希表和 PSCustomObject 的 New-ADUser 之间的区别
- javascript - locomotive-scroll 用于平滑滚动 - 身体不完全显示
- php - 如何在yii2的radioList()中添加不同的通用类
- terraform - 删除数据库后 Terraform 尝试读取 SQL 用户
- powershell - 我应该怎么做才能正确安装cordova?
- android - ConstraintLayout - Ellipsize 开始不起作用
- python - matplotlib 不绘制最后一个数据点