각도 2의 인터페이스로 작업하려고합니다. 인터페이스와 구성 요소를 만들었습니다.각도 2 인터페이스
인터페이스 :
export interface Items {
id: number;
title: string;
message: string;
done: boolean;
}
구성 요소 :
export class AddComponent implements OnInit {
currentItem: string;
todos: any;
constructor(private router: Router) {
this.currentItem = (localStorage.getItem('currentItem')!==null) ? JSON.parse(localStorage.getItem('currentItem')) : [ ];
this.todos = this.currentItem;
}
addTodo(item: Items) {
this.todos.push({
id: item.id,
title: item.title,
message: item.message,
done: false
});
item.title = '';
item.message = '';
localStorage.setItem('currentItem', JSON.stringify(this.todos));
console.log('retorno: ' + this.todos.title + ' titulo: ' + item.title);
this.router.navigate(['./list']);
}
ngOnInit() {}
}
HTML : EXCEPTION: Cannot read property 'id' of undefined
이
사람이 그것을 해결하는 방법을 알고 있나요 :
<div class="container">
<form (submit)="addTodo()">
<div class="form-group">
<label>Id:</label>
<input [(ngModel)]="id" class="textfield form-control" name="id">
</div>
<div class="form-group">
<label>Titulo:</label>
<input [(ngModel)]="title" class="textfield form-control" name="title">
</div>
<div class="form-group">
<label>Mensagem:</label>
<input [(ngModel)]="message" class="textfield form-control" name="message">
</div>
<button type="submit" class="btn btn-success">Salvar</button>
</form>
</div>
I 오류가 있나요?
감사합니다. 그것은 작동합니다. 이런 방식으로 인터페이스를 할 필요가 없습니다. –
사실,이 방법으로는 인터페이스가 필요 없습니다. 구성 요소에 필드를 직접 두는 대신 데이터를 저장하기 위해 인터페이스/개체를 사용하는 것이 더 깨끗할 수도 있습니다. 그러나 두 가지 방법 모두 물론 작동합니다. –
감사합니다. 이제 인터페이스를 사용하여 배우려고 노력할 것입니다. –