Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.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
Angular 2基于其他服务的http调用url_Angular - Fatal编程技术网

Angular 2基于其他服务的http调用url

Angular 2基于其他服务的http调用url,angular,Angular,因此,我有一个组件,它在服务中设置selectedCountry。我还有另一个服务,它从api/${selectedCountry}获取数据,我希望该服务在国家发生变化时订阅,从而反映数据的变化。有办法吗 以下是我目前拥有的: private url: string; private headers: Headers; private options: RequestOptions; constructor(private http: Http, private settingServ

因此,我有一个组件,它在服务中设置
selectedCountry
。我还有另一个服务,它从
api/${selectedCountry}
获取数据,我希望该服务在国家发生变化时订阅,从而反映数据的变化。有办法吗

以下是我目前拥有的:

private url: string;
private headers: Headers;
private options: RequestOptions;

constructor(private http: Http,
    private settingService: SettingService) {
    this.url = `/api/${this.settingService.selectedCountry}`;
    this.headers = new Headers({ 'Content-Type': 'application/json' });
    this.options = new RequestOptions({ headers: this.headers });
}

getTasks(): Observable<Task[]> {
    return this.http.get(this.url)
        .map(response => response.json())
        .catch(this.handleError);  
}
私有url:string;
私有头:头;
私人选择:请求选择;
构造函数(私有http:http,
私人设置服务:设置服务){
this.url=`/api/${this.settingService.selectedCountry}`;
this.headers=新的头文件({'Content-Type':'application/json'});
this.options=newrequestoptions({headers:this.headers});
}
getTasks():可观察{
返回this.http.get(this.url)
.map(response=>response.json())
.接住(这个.把手错误);
}

请让我知道怎么做,或者如果我只是想得太多了。谢谢

您需要做的是从第二个服务(
SettingService
)订阅第一个服务(我假设您粘贴了它的代码片段),并观察对那里的
selectedCountry
属性的更改,然后在有更改后运行代码

设置服务
。。。
从'rxjs/Subject'导入{Subject}
从'rxjs/Rx'导入{Observable}
...
导出类设置服务{
...
//可观测串源
private selectedCountrySource=新主题()
//可观测字符串流
selectedCountryChange$=this.selectedCountrySource.asObservable()
...
//无论您在何处更改所选国家/地区,请添加下一行
this.selectedCountrySource.next(selectedCountry)
//这样做的目的是发出变化。所选国家/地区
//参数将是新的国家/地区
}
其他服务
私有url:string;
私有头:头;
私人选择:请求选择;
构造函数(私有http:http,私有设置服务:设置服务){
this.url=`/api/${this.settingService.selectedCountry}`;
this.headers=新的头文件({'Content-Type':'application/json'});
this.options=newrequestoptions({headers:this.headers});
//订阅设置服务进行更改
sessionService.selectedCountryChange$。订阅(
newSelectedCountry=>{
this.url=`/api/${newSelectedCountry}`;
//如果需要更新,请致电getTasks
this.getTasks()
})
}
getTasks():可观察{
返回this.http.get(this.url)
.map(response=>response.json())
.接住(这个.把手错误);
}

您需要做的是从第二个服务(
SettingService
)订阅第一个服务(
SettingService
),并观察对
selectedCountry
属性的更改,然后在出现更改后运行代码

设置服务
。。。
从'rxjs/Subject'导入{Subject}
从'rxjs/Rx'导入{Observable}
...
导出类设置服务{
...
//可观测串源
private selectedCountrySource=新主题()
//可观测字符串流
selectedCountryChange$=this.selectedCountrySource.asObservable()
...
//无论您在何处更改所选国家/地区,请添加下一行
this.selectedCountrySource.next(selectedCountry)
//这样做的目的是发出变化。所选国家/地区
//参数将是新的国家/地区
}
其他服务
私有url:string;
私有头:头;
私人选择:请求选择;
构造函数(私有http:http,私有设置服务:设置服务){
this.url=`/api/${this.settingService.selectedCountry}`;
this.headers=新的头文件({'Content-Type':'application/json'});
this.options=newrequestoptions({headers:this.headers});
//订阅设置服务进行更改
sessionService.selectedCountryChange$。订阅(
newSelectedCountry=>{
this.url=`/api/${newSelectedCountry}`;
//如果需要更新,请致电getTasks
this.getTasks()
})
}
getTasks():可观察{
返回this.http.get(this.url)
.map(response=>response.json())
.接住(这个.把手错误);
}

设置服务
设置为可注入的,并将
selectedCountry
定义为一个字段,或许可以?谢谢您的快速回复。但是
${this.settingService.selectedCountry}
一直以null返回:/Make the
settingService
为一个可注入的,并将
selectedCountry
定义为一个字段?谢谢您的快速回复。但是
${this.settingService.selectedCountry}
一直以null返回:/
...
import { Subject } from 'rxjs/Subject'
import { Observable } from 'rxjs/Rx'
...

export class SettingService{
    ...
    // Observable string sources
    private selectedCountrySource = new Subject<string>()

    // Observable string streams
    selectedCountryChange$ = this.selectedCountrySource.asObservable()
    ...

    //Wherever you are changing the selected country add the next line
    this.selectedCountrySource.next(selectedCountry)

   //What this does is emit that there is a change. The selectedCountry    
   //parameter will be the new country
}
private url: string;
private headers: Headers;
private options: RequestOptions;

constructor(private http: Http, private settingService: SettingService) {
    this.url = `/api/${this.settingService.selectedCountry}`;
    this.headers = new Headers({ 'Content-Type': 'application/json' });
    this.options = new RequestOptions({ headers: this.headers });

    //Subscribe to the SettingService for changes
    sessionService.selectedCountryChange$.subscribe(
        newSelectedCountry => {
           this.url =`/api/${newSelectedCountry}`;

           //Call getTasks if needed to update
           this.getTasks() 
    })
}

getTasks(): Observable<Task[]> {
    return this.http.get(this.url)
    .map(response => response.json())
    .catch(this.handleError);  
}