내가 필요로하는 메소드를 호출한 후에서 데이터를 얻을 수 있는 http 요청 게시
**서비스:요청을 합니다.서비스입니다.TS**
get_categories(number){
this.http.post( url, body, {headers: headers, withCredentials:true})
.subscribe(
response => {
this.total = response.json();
}, error => {
}
);
}
성:카테고리입니다.TS
search_categories() {
this.get_categories(1);
//I need to call a Method here after get the data from response.json() !! e.g.: send_catagories();
}
경우에만 작동 변경하기:
**서비스:요청을 합니다.서비스입니다.TS**
get_categories(number){
this.http.post( url, body, {headers: headers, withCredentials:true})
.subscribe(
response => {
this.total = response.json();
this.send_catagories(); //here works fine
}, error => {
}
);
}
그러나 나는 메소드를 호출할 필요가send_catagories()
의 내부 구성 요소한 후에 호출이다.get_categories(1);
이
성:카테고리입니다.TS
search_categories() {
this.get_categories(1);
this.send_catagories(response);
}
내가 무엇을 잘못입니까?
업데이트get_categories()
방법을return총(에 싸여 observable):
// Note that .subscribe() is gone and I've added a return.
get_categories(number) {
return this.http.post( url, body, {headers: headers, withCredentials:true})
.map(response => response.json());
}
에서search_categories()
,구독할 수 있습니다 관찰할 수 있는 반환에 의해get_categories()
(또는 당신을 유지할 수 있 변환하여 체인이 더 RxJS 사업자):
// send_categories() is now called after get_categories().
search_categories() {
this.get_categories(1)
// The .subscribe() method accepts 3 callbacks
.subscribe(
// The 1st callback handles the data emitted by the observable.
// In your case, it's the JSON data extracted from the response.
// That's where you'll find your total property.
(jsonData) => {
this.send_categories(jsonData.total);
},
// The 2nd callback handles errors.
(err) => console.error(err),
// The 3rd callback handles the "complete" event.
() => console.log("observable complete")
);
}
참고는 당신만 일단 가입,니다.
내가 말했듯이,코멘트에는.구독()
에서의 관찰을 받 3 콜백은 다음과 같다:
obs.subscribe(
nextCallback,
errorCallback,
completeCallback
);
그들에 전달되어야 합니다. 지't 을 통과해야합니다. 많은 시간만nextCallback
이 구현되:
obs.subscribe(nextCallback);
을 추가할 수 있습을 콜백 함수의 목록 get_category(...)매개 변수입니다.
Ex:
get_categories(number, callback){
this.http.post( url, body, {headers: headers, withCredentials:true})
.subscribe(
response => {
this.total = response.json();
callback();
}, error => {
}
);
}
그리고 당신은 전화 할 수 있 get_category(...)이와 같은:
this.get_category(1, name_of_function);
할 수 있는 코드는 람다 식으로 세 번째 매개 변수(에 완료하)구독 방법입니다. 여기에 내가 다시 설정 departmentModel 변수를 기본값으로 되돌릴 수 있습니다.
saveData(data:DepartmentModel){
return this.ds.sendDepartmentOnSubmit(data).
subscribe(response=>this.status=response,
()=>{},
()=>this.departmentModel={DepartmentId:0});
}
당신이 할 수 있는 이를 사용하여 새로운대상
,'너무:
타이프 라이터:
let subject = new Subject();
get_categories(...) {
this.http.post(...).subscribe(
(response) => {
this.total = response.json();
subject.next();
}
);
return subject; // can be subscribed as well
}
get_categories(...).subscribe(
(response) => {
// ...
}
);