html - 复选框加载但未点击 HTML Angular
问题描述
有一个奇怪的问题,复选框被加载了 id 和值但没有被点击。也没有显示错误。
<button [popover]="columnConfigJobDetails"><span class="icon-cog"></span></button>
<popover-content #columnConfigJobDetails [closeOnClickOutside]="true">
<ng-container *ngFor='let col of columnDropdownStates'><form>
<label *ngIf='col' class="checkbox ">
<input type="checkbox" id={{col.field}} [checked]="col.visible"
name={{col.header}} (change)="onColumnChange($event)"/>
<span class="checkbox__input"></span>
<span class="checkbox__label">{{col.header}}</span>
</label></form>
</ng-container>
</popover-content>
我的 Angular/typescript 代码看起来像一个对象数组。
this.columnDropdownStates -:
0: {header: "Name", field: "name", visible: true}
1: {header: "Devices Enabled", field: "enabledDeviceNumber", visible: true}
2: {header: "Description", field: "description", visible: true}
新更新 - 我确实收到此错误,但不确定它是什么意思
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: undefined'. Current value: 'ngIf: true'. It seems like the view has been created after its parent and its children have been dirty checked. Has it been created in a change detection hook ?
解决方案
There are two possible fixes,
If you are initializing or assigning your
*ngIf
values on thengOnInit()
you have to move it tongAfterViewInit()
as at this point the change detection doesn’t work on the ngOnInit(). Another reason is, in most cases, your view is probably not initialized in the ngOnInit().If you are using Angular version of above 8, you might have to set a static flag while creating your
@ViewChild
as it is mandatory from v8. For example,@ViewChild(TemplateRef, { static: }) templateRef : TemplateRef;
The true or false values depends on the following situations,
{ static: true }
if you are accessing ViewChild
in ngOnInit
{ static: false }
if you are accessing in ngAfterViewInit
. This should also be used in your scenario where you are using NgIf, NgFor, NgSwitch
or other structural directives.
Note: In Angular 9, the static flag is defaulted to false.
For more details please refer this official discussion.
推荐阅读
- python - 一些 Tkinter create_line 命令不绘制垂直线,但其他命令
- scikit-learn - 在 scikit-learn 中使用 GridSearchCV 会导致 CalibratedClassifierCV 出现错误。GridSearchCV 对象没有属性 'best_params_
- hive - Apache Beam SqlTransforms 架构问题
- laravel-5.6 - 在 laravel 上创建第一个数据库表的问题
- laravel - laravel 中的单页
- python - 根据条件更改多维numpy数组值
- node.js - 带有节点 js 的 AWS RDS:错误:在参数中找到意外的键“DeleteProtection”
- angular - Angular 4+ - 如何从输入日期选择器中获取日期
- ios - 访问 Node Express 端点时,URLSession.shared.dataTask 数据始终为空
- javascript - map、filter 与 for 和 forEach 之间的区别以及使用 map 和 filter 优于 For 和 forEach 循环的优势是什么?