检查值后表达式已更改

Expression has changed after it was checked value

检查后表达发生了变化。上一个 value: 'ngIf: false'。当前 value: 'ngIf: true'

<div class="editable comment-msg" *ngIf="c_editId == c.id"> 
 <form [formGroup]="editCommentForm" class="edit-form">
    <div class="edit-message">
      <textarea
        rows="3"
        type="text"
        class="form-control"
        formControlName="message"
        placeholder="Enter Comment "
        value="{{ c.message }}"
        maxlength="1024"
        #message>
      </textarea>
    <div class="custom-content-padding">
     <small class="form-text text-muted custom-display">
      <code>{{ message.value.length }}</code> of 1024 characters
     </small>
    <span *ngIf="message.value.length != 0" class="alert-success">
       <i class="fa fa-check" aria-hidden="true"></i> Your edit is good to go!
    </span>
  </form>
 <div>
 ...

this.commentForm = this.formBuilder.group({
    topicId: [this.topicId, Validators.required],
    message: [
      "",
      Validators.compose([Validators.required, Validators.maxLength(1024)])
    ]
  });

  this.editCommentForm = this.formBuilder.group({
    message: [
      "",
      Validators.compose([Validators.required, Validators.maxLength(1024)])
    ]
  });
  this.getCommentsCommentPage();
});



  editCommentToggle(c: Comment) {

   this.attachments2 = [];
   var id = String(c.id)
   document.getElementById(id).style.display = "none";

   this.setTitleEdit = true;
   this.c_editId = c.id;
   this.comment = c;
   this.deleteFilesCheckboxes.length = 0;

   if (this.filepath[c.id] != null) {
    for (let file of this.filepath[c.id]) {
     this.deleteFilesCheckboxes.push(new FileToCheckbox(false, file));
     }
   }
}

单击评论上方的编辑按钮时,第 1 行中的 *ngIf 语句变为真。编辑中的评论总是以至少一个字符开头。

第二个*ngIf就是问题所在。显示上述错误,但代码运行正常。我看到其他帖子有类似的错误标题,但无法应用错误或修复我的问题。

我猜你的 setTitleEdit 有一些检查问题。

要解决此问题,请将 ChangeDetectorRef 添加到您的构造函数中

constructor(private changeRef: ChangeDetectorRef)

然后在setTitleEdit下调用detectChanges

editCommentToggle(c: Comment) {

   this.attachments2 = [];
   var id = String(c.id)
   document.getElementById(id).style.display = "none";

   this.setTitleEdit = true;
   this.changeRef.detectChanges();
   this.c_editId = c.id;
   this.comment = c;
   this.deleteFilesCheckboxes.length = 0;

   if (this.filepath[c.id] != null) {
    for (let file of this.filepath[c.id]) {
     this.deleteFilesCheckboxes.push(new FileToCheckbox(false, file));
     }
   }
}