我的服务第一次运作良好。当它再次被调用时,它不起作用。我在NgModule如何在一个组件中订阅Angular 2中的服务几次?
服务这里是我的代码
this._portService.getPortList().subscribe(
(data) => {
this.len = data['interfaces-state'].length;
for (this.i = 0; this.i<this.len;this.i++){
this.portListJson.push(data['interfaces-state'][this.i].interface);
}
this.removeONUPorts();
this.addNames();
});
我的构造
constructor(private _portService:PortsService, private elRef: ElementRef){
}
当我尝试调用不同的功能不起作用相同的代码。
我的服务
getPortList(){
return this._http.get(environment.api + "restconf/data/ietf-interfaces:interfaces-state/interface?fields=")
.map(response => response.json());
};
我的第二个电话
setONUList(port: Port){
this.clearLists();
this._portService.getPortList().subscribe(
(data) => {
this.len = data['interfaces-state'].length;
for (this.i = 0; this.i<this.len;this.i++){
this.portListJson.push(data['interfaces-state'][this.i].interface);
}
this.removeONUPorts();
this.addNames();
});
}
更新问题与您的第二次订阅现在它似乎很好 –
你是否在getPortList()中调用正确的URL。您可以查看通过安装返回的数据。像尝试这样来检查:'getPortList(){ 返回this._http.get(environment.api +“restconf/data/ietf-interfaces:interfaces-state/interface?fields =”) .map(response => response 。上传.json())做(数据=>的console.log(JSON.stringify(数据))); };' –
我做了,它不订阅。在调试时跳过那部分 – Erad