Skip to content

Commit 4771e59

Browse files
author
maksym.koretskyi
committed
Task: Components input/output communication & ExpressionChangedAfterItHasBeenCheckedError
1 parent 45c1671 commit 4771e59

File tree

4 files changed

+30
-27
lines changed

4 files changed

+30
-27
lines changed

src/app/todo-item.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,7 @@ import { Component, EventEmitter, Input, Output } from '@angular/core';
99
Completed
1010
<input id="checkBox" type="checkbox" (change)="complete($event)">
1111
</label>
12-
`,
13-
styles: [':host {display: block; width: 90%; border: 2px solid green; padding: 20px}']
12+
`
1413
})
1514
export class TodoItemComponent {
16-
@Input() item;
17-
@Output() completed = new EventEmitter();
18-
19-
complete(event) {
20-
this.completed.emit(event.target.checked);
21-
}
22-
23-
ngAfterViewInit() {
24-
// why does this line cause ExpressionChangedAfterItHasBeenCheckedError
25-
// this.item.completed = !this.item.completed;
26-
27-
// why this error disappears if you remove
28-
// <h4>Status: {{item.completed}}</h4>
29-
// from this component's template
30-
}
3115
}

src/app/todo-list.component.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,7 @@ import { Component } from '@angular/core';
55
template: `
66
<h1>To-do list</h1>
77
<todo-item [item]="todoItem" (completed)="updateCompletedStatus($event)"></todo-item>
8-
`,
9-
styles: [':host {display: block; width: 500px; border: 2px solid blue; padding: 20px}']
8+
`
109
})
1110
export class TodoListComponent {
12-
todoItem = {
13-
title: 'Finish homework',
14-
completed: false
15-
};
16-
17-
updateCompletedStatus(completed) {
18-
this.todoItem.completed = completed;
19-
}
2011
}

task.jpg

28.4 KB
Loading

task.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Components input/output communication & ExpressionChangedAfterItHasBeenCheckedError
2+
---------------------
3+
4+
Steps
5+
---------------------
6+
7+
* Implement ToDoList component that contains one child componentn ToDoItem component.
8+
Here is a template of the component:
9+
<h1>To-do list</h1>
10+
<todo-item [item]="todoItem" (completed)="updateCompletedStatus($event)"></todo-item>
11+
12+
* Implement ToDoItem component that takes a to-do item as an input. It shows
13+
title and status of the to-do item that it takes. It also contains a checkbox
14+
to switch status of the to-do item.
15+
16+
Here is a template of the component:
17+
<h4>Title: {{item.title}}</h4>
18+
<h4>Status: {{item.completed}}</h4>
19+
<label>
20+
Completed
21+
<input id="checkBox" type="checkbox" (change)="complete($event)">
22+
</label>
23+
24+
* ToDoItem component also has `completed` event that is listened to by
25+
the ToDoList component. Once it receives the notification, it udpates the status
26+
of the to-do item.
27+
28+
* Add styles with borders and padding according to the task.jpg

0 commit comments

Comments
 (0)