tastejs / todomvc

Helping you select an MV* framework - Todo apps for React.js, Ember.js, Angular, and many more
http://todomvc.com
Other
28.65k stars 13.77k forks source link

[Angular] Expression Changed After Checked Error #2258

Open minhcu opened 3 months ago

minhcu commented 3 months ago

https://github.com/tastejs/todomvc/blob/master/examples/angular/src/app/todo-item/todo-item.component.ts https://github.com/tastejs/todomvc/blob/master/examples/angular/src/app/todo-item/todo-item.component.html

So I have copied this exact code and run it using Angular 18. When the input is focused, the title property is changed, resulting in this error

handleFocus(e: Event) {
    this.title = this.todo.title;
  }

Expression Changed After Checked https://angular.dev/errors/NG0100

It makes total sense that the title is changed in afterViewChecked hook creating this error. So is this a good practice? Is there any better way to set up the title like when the todo is set or use onChanges hook?

@Input() set todo(data: Todo) {
    this._todo = data;
    this.title = this.todo.title;
}