【发布时间】:2018-05-29 19:02:44
【问题描述】:
我有一个表,它调用一个名为 modal 的子组件,modal 组件有两个按钮保存和取消,用于内联编辑。我知道我必须使用“ChangeDetectorRef”,但我不知道如何使用我的代码使用事件“ngAfterViewInit”
ExpressionChangedAfterItHasBeenCheckedError:表达式已更改 检查后。以前的值:'disableEditSaveButton: false'。 当前值:'disableEditSaveButton: true'。
dashboard.HTML
<p-table #dt [value]="iToDoList" dataKey="id" [paginator]="true" [rowsPerPageOptions]="[10,50,100]"
[rows]="10">
<ng-template pTemplate="header">
<tr>
<th>ID</th>
<th>Comment</th>
<th>Action</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-row>
<tr>
<td>{{row.id}}</td>
<td>
<div *ngIf="!row.isEditable">{{row.comment}}</div>
<div *ngIf="row.isEditable">
<input type="text" [(ngModel)]="row.comment">
<span *ngIf="isEmpty(row.comment)" style="color:crimson">Required</span>
</div>
</td>
<td>
<div>
<modal [disableEditSaveButton]='disableSaveButton' (open)="onOpen(row)" [showModal]="!row.isEditable" (selectedRow)="onSelectedRow(row)" (cancelEdit)="onCancelEdit(row)"></modal>
</div>
<!--<button (click)="editRow(row)">Edit</button>-->
</td>
<td> <button (click)="save(row)">Save</button></td>
</tr>
</ng-template>
</p-table>
dashboard.compnent(这会导致 ExpressionChangedAfterItHasBeenCheckedError)
isEmpty(input) {
if (input.replace(/\s/g, '') === "") {
this.disableSaveButton = true;
}
else {
this.disableSaveButton = false;
}
// this.cdr.detectChanges();
return input.replace(/\s/g, '') === "";
}
modal.html
<div *ngIf='!showModal'>
<button type="button" class="btn-xs btn-primary" (click)="onSave()" [disabled]='disableEditSaveButton'>Save</button>
<button type="button" class="btn-xs btn-orange" (click)="onCancelEdit()">Cancel</button>
</div>
modal.component
@Input() disableEditSaveButton: boolean = false;
******************更新****************************** ******************************************************
浏览器仍然抛出 ExpressionChangedAfterItHasBeenCheckedError
组件
isEmpty(input) {
this.cdr.detach();
if (input.replace(/\s/g, '') === "") {
this.disableSaveButton = true;
}
else {
this.disableSaveButton = false;
}
// this.cdr.detectChanges();
// restart change detection for this view
this.cdr.reattach();
return input.replace(/\s/g, '') === "";
}
【问题讨论】:
-
我看到了这篇文章,我试着做 ngAfterViewInit() { this.cdr.detectChanges();它仍然不起作用,因为我设置的子输入不在 ngAfterViewInit 内。如果需要在“isEmpty”方法中设置子值,不知道该怎么做
-
我使用了 setTimeout,它工作但很慢...我需要一个更好的解决方案
标签: angular typescript primeng