0
如何从服务获取组件更新值。下面我试图从ConnectService获取showValues到MasterComponent。Angular 2如何从服务获取组件的最新更新值
export class MasterComponent{
public showValue:string;
constructor(public connectService: ConnectService, public _router: Router){
if(localStorage.getItem("clientStatus")=='connected'){
this.showValue = this.connectService.showValues;
console.log("MasterComponent",this.showValue);
}
}
从下面的服务我想更新组件。
export class ConnectService{
public showValues: string;
constructor(){
}
recvNotify(m) {
//getting value
buttonName = innervalue;
console.log("ELSEinnervalue",buttonName);
switch (buttonName) {
case "0x01010000":
console.log('showValue==1');
this.showValues = "1";
break;
case "0x01020000":
console.log('showValue==2');
this.showValues = "2";
break;
default:
console.log('showValue==3');
this.showValues = "3";
}
}
}
我想刚刚更新从我服务的变量使用
Subject
从rxjs
然后。是否有任何简单的方法来做到这一点 – NMNB
这是基本的方式,只有两件事可以用作通知者,是EventEmitter,第二种是使用Subject的Observable –