使用可观察对象从 post 方法获取返回值

Get returned value from post method with observables

我需要 post 方法的返回值。我尝试映射数据,但它 (newTask) 保持为空。我不知道如何从这个函数中获取 Tasks 对象。请帮忙。

    public PostTask(task:Tasks){

        let headers=new Headers({'Content-Type':'application/json'});
        let options=new RequestOptions({headers:headers});


       this._http.post('http://localhost:58592/api/tasks',task,options)
            .map(res => res.json() as Tasks)
            .map(newTask=>this.newTask=newTask) 
            .catch(this.handleError).subscribe();


    }

http.get() returns 可观察。要在发射后从 Observable 中提取值,我们应该在转换发射值后订阅它。

this._http.post('http://localhost:58592/api/tasks',task,options)
        .map(res => res.json() as Tasks)
        .subscribe( 
                newTask =>  this.newTask = newTask ,
                error => console.log(error);
        );   

请通读 angular.io 文档和示例。