小编Soj*_*jye的帖子

Angular2 - 如何在组件中链接异步服务调用(http请求)?

我有一个组件,首先需要调用POST的服务.然后在同一个组件中我想等到POST完成后,调用另一个获取数据的服务.

如何进行GET调用等待POST调用完成?

在new-version.component.ts中:

private createNewVersion(value) {
    ...

    // create new version, then call on all available versions

    // POST call
    this._newVersionService.createNewVersion(vnr);

    // GET call
    this._versionService.getAvailableVersions(); 

    ...
}
Run Code Online (Sandbox Code Playgroud)

在new-version.service.ts中:

export class NewVersionService {

response$: Subject<any>;

constructor(private _http: Http) {
    this.response$ = new BehaviorSubject<any>(null);
 }

public createNewVersion(versionNr) {    
    this._http.post('http://localhost:8080/services/' + versionNr, null, {
        method: 'POST',
    })
    .subscribe(response => {
        this.response$.next(response.status);
    },
    error => console.error(error));
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

asynchronous http chaining angular

10
推荐指数
2
解决办法
2万
查看次数

Angular 2 - 动态查找要在http请求(服务)中使用的基本URL

我想知道是否有一种动态的方式来获取基本网址,以便在http请求中使用?

有没有办法动态获取http://192.123.24.2:8080

public getAllTickets() {
    this._http.get('http://192.123.24.2:8080/services/', {
        method: 'GET',
        headers: new Headers([
            'Accept', 'application/json',
            'Content-Type', 'application/json'
        ])
    })
Run Code Online (Sandbox Code Playgroud)

所以,我的请求看起来像:

public getAvailableVersions() {
    this._http.get('../services', {
        method: 'GET',
        headers: new Headers([
            'Accept', 'application/json',
            'Content-Type', 'application/json'
        ])
    })  
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种不必硬编码REST调用URL的方法.或者是具有URL的全局变量的唯一选择?

谢谢!

rest url service http angular

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

angular ×2

http ×2

asynchronous ×1

chaining ×1

rest ×1

service ×1

url ×1