2
我正在玩一些角度2.到目前爲止,我構建了一個全局服務,它包含一個接口。其他組件正在使用此全局服務的界面。如果接口通過組件更改,那麼界面也將更改爲子組件。angular2 - 通過自定義管道使用全局服務
現在我試圖通過管道來處理這個問題。但是當我通過子組件更改接口值時,其他組件中的接口值不會更改。
這是我走到這一步:
import { Pipe, PipeTransform, EventEmitter } from '@angular/core';
import { GlobalService } from './global-service'
import { MyInterface } from './my-interface'
@Pipe({name: 'myPipe'})
export class MyPipe implements PipeTransform {
private value: string;
private _interface: MyInterface;
private interfaceChanged: EventEmitter<MyInterface>;
constructor(private globalService: GlobalService) {
this._interface = globalService._interface;
this.interfaceChanged = this.globalService
.interfaceChanged
.subscribe((newInterface: MyInterface) => {
this._interface = newInterface;
});
}
transform(value: string, args: any[]): string {
for (var key in this.language) {
if (key == value) {
this.value = this._interface[key];
break;
}
}
return this.value;
}
}
這也是當值或參數改變Plunker
不錯,非常感謝! – user2741109