Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度2:如何在从subscribe http.post获得响应后调用函数_Javascript_Angular_Typescript_Promise_Subscribe - Fatal编程技术网

Javascript 角度2:如何在从subscribe http.post获得响应后调用函数

Javascript 角度2:如何在从subscribe http.post获得响应后调用函数,javascript,angular,typescript,promise,subscribe,Javascript,Angular,Typescript,Promise,Subscribe,我需要在从HTTPPOST请求获取数据后调用一个方法 服务:request.service.TS get_categories(number){ this.http.post( url, body, {headers: headers, withCredentials:true}) .subscribe( response => { this.total = response.json(); }, error => { }

我需要在从HTTPPOST请求获取数据后调用一个方法

服务:request.service.TS

get_categories(number){
 this.http.post( url, body, {headers: headers, withCredentials:true})
    .subscribe( 
      response => {
        this.total = response.json();

      }, error => {
    }
  ); 

}
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 => {
    }
  ); 

}
组件:类别。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();
}
仅当我更改为:

服务:request.service.TS

get_categories(number){
 this.http.post( url, body, {headers: headers, withCredentials:true})
    .subscribe( 
      response => {
        this.total = response.json();

      }, error => {
    }
  ); 

}
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 => {
    }
  ); 

}
但是在调用this.get\u categories(1)之后,我需要在组件内部调用方法
send\u categories()
像这样

组成部分:categories.TS

search_categories() {

this.get_categories(1);
this.send_catagories(response);
}

我做错了什么?

您可以将回调函数添加到get_category(…)参数列表中

例:

然后您可以这样调用get_category(…):

this.get_category(1, name_of_function);
然后


更新您的
get_categories()
方法,以返回总数(包装在可观察项中):

//注意.subscribe()已经不存在了,我添加了一个return。
获取类别(编号){
返回this.http.post(url,body,{headers:headers,withCredentials:true})
.map(response=>response.json());
}
search\u categories()
中,您可以订阅
get\u categories()
返回的可观察对象(或者您可以通过链接更多RxJS操作符来继续转换它):

//现在在get\u categories()之后调用send\u categories()。
搜索类别(){
此。获取_类别(1)
//.subscribe()方法接受3个回调
.订阅(
//第一次回调处理可观察对象发出的数据。
//在您的例子中,是从响应中提取的JSON数据。
//在那里你可以找到你的全部财产。
(jsonData)=>{
此.send_类别(jsonData.total);
},
//第二个回调处理错误。
(错误)=>控制台错误(错误),
//第三个回调处理“complete”事件。
()=>console.log(“可观察到的完成”)
);
}
请注意,最后只订阅一次

正如我在评论中所说,任何observable的
.subscribe()
方法都会接受如下3种回调:

this.get_category(1, name_of_function);
obs.subscribe(
下一回,
错误回调,
完全回调
);
它们必须按此顺序通过。你不必三门都及格。很多时候,只执行
nextCallback

obs.subscribe(nextCallback);

您可以将lambda表达式编码为subscribe方法的第三个参数(完成时)。这里我将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) => {
     // ...
   }
);

谢谢,我会试试的。但我的想法是用更专业的方法来捕捉回应。例如:获取第(1)类。然后是一个可观察的,但我不知道怎么做。使用回调函数没有什么不专业的地方,但适合你自己。在我看来,在使用可观察的时将回调传递给服务不是一个好模式。正如Louis所提到的,它使额外的逻辑和错误处理变得丑陋。@RobM我只是想说你是对的。在重写一个同事的应用程序时,我去了一个又一个的回调地狱,现在我看到了我的错误!
send_categories()
是否也使用可观察的?如果是,则需要使用
.mergeMap()
操作符将可观察对象从get_categories()链接到send_categories()中的可观察对象。如果您需要有关语法的帮助,请告诉我。send_catagories()没有使用observable,请告诉我语法:return this.http.post(url,body,{headers:headers,withCredentials:true})。订阅(response=>{this.total_page=response.json();return this.total_page;},.share());}然后是this.get_category(1.subscribe)(response=>{this.callfunc();});知道了。我已经发布了一个语法正确的答案。是的!,这就是我想要的,如何在没有“.map”的情况下保持旧语法,例如:返回this.http.post(url,body,{headers:headers,withCredentials:true}).subscribe(response=>{this.total=response.json();return total;}).share();(173,69):错误TS2339:类型“Subscription”上不存在属性“subscripte”。为什么需要原始语法?FRP是关于组合流来创建一个可以订阅的有意义的可观察对象。@pixelbits我收到一条错误消息,
.share在类型observable
上不存在,并且
应该返回total
而不是
返回这个。total
?我只是想弄明白这段代码,因为我正在努力解决类似的问题。谢谢,谢谢,谢谢!它工作得很好!这里有两个问题,1)send_categories(total){this.myNewTotal=total}如何从“total”参数提取数据(responde.json)?2) 我在语法中添加了“error”:map(response=>response.json()),error=>{};如果我需要调用此。仅在出现错误的情况下发送类别(总数),我如何订阅错误?嗯。根据您以前的代码,我认为
response.json()
是总数。我的意思是,我将变量命名为
total
,但它包含
response.json()
中的任何内容;您可以随意命名此变量。我更新了我的答案,以更好地反映这一点,并向您展示如何处理错误。我可能遗漏了subscribe中第三个参数的要点,但我想知道您是否可以调用一个方法来完成回调?通常这两个回调是同义的。以HTTP请求为例。当服务器返回数据时,会调用
nextCallback
,然后在
completeCallback
之后立即调用。但是在不同的情况下(例如聊天系统),
nextCallback
可以被多次调用(例如,对于每个聊天通知),
completeCallback
永远不能被调用。这是有史以来最好的解释。@Pratik这是什么意思?它的意思是第一句话让subject=newsubject();giv