[英]Angular 2 view not updating after I update a record in the database
我有一个使用Angular 2的简单CRUD应用程序。主页显示用户可以创建,编辑或删除的卡片列表。
在卡片编辑页面上,当我单击提交时,它称为:
卡edit.component.ts
onSubmit() {
this.updateCard(this.card);
alert("Card updated!");
this._router.navigate(['/CardList']);
}
updateCard(updatedCard: Card) {
// call to card.service.ts
this._cardService.updateCard(updatedCard)
.subscribe(
result => this.response = result,
error => this.errorMessage = <any>error);
}
card.service.ts
updateCard(updatedCard: Card): Observable<string> {
let body = JSON.stringify(updatedCard);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this._http.put(this._cardUrl + "/" + updatedCard.id, body, options)
.map(this.extractData)
.catch(this.handleError);
}
卡已在数据库中更新。 然后路由器将我带回到卡列表。
卡list.component.ts
import { Component, OnInit} from 'angular2/core'
import { ROUTER_DIRECTIVES } from 'angular2/router'
import { Card } from './card'
import { CardService } from './card.service'
import { CardFilterPipe } from './card-filter.pipe'
@Component({
templateUrl: 'app/card/card-list.component.html',
directives: [ROUTER_DIRECTIVES],
pipes: [CardFilterPipe]
})
export class CardListComponent implements OnInit {
pageTitle: string = 'Card List';
cards: Card[];
errorMessage: string;
listFilter: string;
constructor(private _cardService: CardService) { }
ngOnInit(): void {
this.getCards();
}
getCards() {
this._cardService.getCards()
.subscribe(
result => this.cards = result,
error => this.errorMessage = <any>error);
}
}
问题:我刚刚在编辑页面上所做的更改未反映在列表视图中。 需要刷新整页才能获得反映最新数据的记录。
我想我知道了。 看来我必须在updateCard()中而不是onSubmit()中调用路由器导航。
updateCard(updatedCard: Card) {
// call to service to update record in database
this._cardService.updateCard(updatedCard)
.subscribe(
result => this.response = result,
error => this.errorMessage = <any>error,
() => this._router.navigate(['CardList'])
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.