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

标签 angular typescript

我是 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/

相关文章:

javascript - JavaScript/TypeScript 中带有字符串的连续分配数组

node.js - Angular-cli 种子项目没有 Bower、Grunt、gulp,它如何管理所有依赖项?

javascript - 如何捕获 Action 窗口:beforeunload Angular 8

angular - 来自 observable : testing with Jasmine marbles 的特殊结果

javascript - 如何知道复选框是否被选中?

Angular :ReferenceError: 'function' 未定义

angular - 类型 'BehaviorSubject<false>' 不可分配给类型 'BehaviorSubject<boolean>'

Angular 单元测试(带 Getter): Error: No value accessor for form control with name

javascript - 使用联合类型的 Typescript 中的重载函数的参数数量和类型不同

typescript - 是否可以检测到不必要的类型保护?