간단한 순층 적용을 위해 각도 2/4 패턴과 관련하여 몇 가지 질문이 있습니다. 대부분의 예제를 보았습니다. 삽입/업데이트 후에 데이터가 어떻게 새로 고쳐지는지 알지 못합니다.각도 관측 값 vs 약속 CRUD 및 데이터 새로 고치기
Observables에서는 약간 새로운 기능이지만 데이터를 새로 고치는 것만 제외하면 대부분의 패턴을 이해합니다. 삽입/업데이트/삭제 후에 누군가가 데이터를 새로 고치는 가장 좋은 방법을 설명 할 수 있으며, 약속 대신 Observables를 사용해야 할 필요가 있습니까? 약속을 사용하는 것이 허용됩니까?
Observables에서 어떻게하면됩니까?
users.service
insertPromise(user : User)
{
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let url = this.baseUrl;
return this.http.post(url, JSON.stringify(user),options)
.do(data=>console.log(JSON.stringify(data)))
.toPromise()
.then(this.extractPromiseData)
.catch(this.handleError);
}
insertObservable(user : User) {
this.usersService.insert(user)
.subscribe(
resp => {
console.log(resp);
},
error => this.errorMessage = <any>error);
}
사용자 list.component은 (삽입 후 새로 고침 포함)
insert(user: User)
{
this.usersService.insertPromise(user)
.then(result=>console.log(result))
.then(
()=>this.usersService.getAllUsers()
.then(
users=>
this.users = users
)
.catch(error=>console.log(error))
)
.catch(error=>console.log(error));
}
insertObservable(user: User)
{
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let url = this.baseUrl;
return this.http.post(url, JSON.stringify(user),options)
.map((response: Response) => response.json())
.do(data=>console.log(JSON.stringify(data)))
.catch(this.handleError);
}
예를 들어 변경된/새 사용자를 다시로드하는 것이 의미가 있지만 전체 목록을로드하는 방법은 무엇입니까? 다음은 사용자를 찾고 해당 사용자를 업데이트 된 사용자로 설정해야합니다. 삭제에 대해서는 사용자를 삭제해야합니까? – Fab
사실, PrimeNG DataTable을 사용하고 있고 스프레드 연산자가 새 것이지만 한 번 예상대로 코드를 변경 한 것으로 나타났습니다. 도와 주셔서 감사합니다!! let users = [... this.users]; users.push (user); this.users = users; – Fab
아마도 DataTable은 새 개체 (배열)를받을 때만 변경을 감지하고 스프레드 연산자는 분명히 새 배열을 만듭니다. – gsc