首页 > 解决方案 > 当我只有一个实例时,Typescript observable 永远不会更新

问题描述

我在这个问题上工作了很多天,但我从来没有发现为什么我的 observable 不起作用:当修改出现时它没有更新..我尝试使用其他 http 请求,但我遇到了同样的问题。

当有新的修改时,如何更新我的角度组件中的数据?

服务 :

@Injectable({
 providedIn: 'root'
})
[...]
getAllCodeBuildrun(projectCodebuildName : string): Observable<Response>{
  return  this._http
    .get(environment.baseURL + "/codebuildProject/"+projectCodebuildName+"/buildRun")
    // ...and calling .json() on the response to return data
    .map((res:Response) => res.json())
    //...errors if any
    .catch((error:any) => Observable.throw(error || 'Server error'));
}

零件:

@Component({
  selector: 'app-application',
  templateUrl: './application.component.html',
  styleUrls: ['./application.component.css']
})
[...]
ngOnInit() {   
  this.applicationsService.getAllCodeBuildrun("test").subscribe(
    buildsRun => {
              console.log('codeBuild :', buildsRun); // appear just one time in console 
              this.builds= buildsRun["builds"];
              }, 
    error => console.error('error', error)
  );
}

应用程序模块.TS

providers: [
   ProjectsService
],

标签: typescripthttpangular2-services

解决方案


推荐阅读