gpt4 book ai didi

Angular 6 - 检查后表达式已更改

转载 作者:太空狗 更新时间:2023-10-29 17:24:32 25 4
gpt4 key购买 nike

我是 Angular 的新手,正在尝试与 easyui angular 结合使用.

我在尝试打开/关闭对话框时发现了这个错误:

ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'klass: l-btn f-inline-row f-content-center l-btn-small l-btn-focus'. Current value: 'klass: l-btn f-inline-row f-content-center l-btn-small'.

这是我的代码。

user.component.html

<eui-linkbutton (click)="addAction()" iconCls="icon-add" style="margin-bottom:10px">Add New</eui-linkbutton>
<eui-datagrid
[data]="users" style="height1:250px" [filterable]="true"
[selectionMode]="'single'"
[(selection)]="rowSelected"
[euiContextMenu]="contextMenu"
>

</eui-datagrid>


<!-- contextmenu -->
<eui-menu #contextMenu (itemClick)="contextClick($event)">
<eui-menu-item value="edit" text="Edit"></eui-menu-item>
<eui-menu-item text="Delete"></eui-menu-item>
<eui-menu-item text="View"></eui-menu-item>
</eui-menu>


<eui-dialog #formDialog [closed]="closed" [draggable]="true" [modal]="true" [title]="isNewRow ? 'Add' : 'Edit'" (close)="closed=true">

<div class="dialog-button">
<eui-linkbutton [disabled]="!formObj.valid" (click)="saveAction()" style="width:80px">Save</eui-linkbutton>
<eui-linkbutton (click)="formDialog.close()" style="width:80px">Cancel</eui-linkbutton>
</div>
</eui-dialog>

user.component.ts

export class UserComponent implements OnInit {
rowSelected = null;
formObj: FormGroup;

constructor(public userService: UserService, fb: FormBuilder){

}

get row(){
return this.rowSelected;
}
set row(value: any){
this.rowSelected = value;
this.formObj.reset(this.rowSelected);
}

editAction(row){
this.row = this.rowSelected;
this.closed = false;
}

addAction(){
console.log('open babe');
this.row = {
'id' : null,
'username' : null,
'password' : null,
'email' : null,
'first_name' : null,
'last_name' : null
};

this.closed = false;
}


isNewRow = false;
editingRow = null;
closed = true;
users: Object;

currentSelection = null;
ngOnInit() {
this.userService.getData().subscribe(
data => this.users = data
)
console.log('ngoninit');

this.initRow();
this.closed = true;
}

initRow() {
this.editingRow = {
username: null,
password: null,
email: null,
first_name: null,
last_name: null,
};
}

onAddRow() {
console.log('open babe');
this.initRow();
this.isNewRow = true;
this.closed = false;
}

onEditRow(row) {
this.isNewRow = false;
this.editingRow = row;
this.closed = false;
}
contextClick(value){
if(!this.rowSelected){
alert('Please select a row first!');
}
else{
if(value == 'edit'){
this.editAction(this.rowSelected);
}
}
}
}

我搜索了很多教程来解决这个问题,但教程条件似乎与我的不符。

我是否误解或遗漏了有关 Angular 实用信息?更新属性的正确方法是什么?

最佳答案

使用ChangeDetectorRef服务检测新变化

当 View 使用OnPush (checkOnce) 更改检测策略时,明确标记 View 已更改,以便再次检查。

import { Component, Input, ChangeDetectionStrategy,ChangeDetectorRef } from '@angular/core';

@Component({
selector: 'component',
templateUrl: 'component.html',
changeDetection: ChangeDetectionStrategy.OnPush

})


constructor(cdRef:ChangeDetectorRef){}
ngAfterViewInit() {

this.cdRef.detectChanges();
}

关于Angular 6 - 检查后表达式已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52922326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com