Typescript 角度2和#xA0–;通过自定义管道使用全局服务
我在玩angular 2。到目前为止,我构建了一个包含接口的全局服务。其他组件正在使用此全局服务的接口。如果通过组件更改接口,则子组件的接口也将更改 现在我正试图通过管道来处理这个问题。但是,当我通过子组件更改接口值时,其他组件中的接口值不会更改 到目前为止,我得到的是:Typescript 角度2和#xA0–;通过自定义管道使用全局服务,typescript,angular,pipe,Typescript,Angular,Pipe,我在玩angular 2。到目前为止,我构建了一个包含接口的全局服务。其他组件正在使用此全局服务的接口。如果通过组件更改接口,则子组件的接口也将更改 现在我正试图通过管道来处理这个问题。但是,当我通过子组件更改接口值时,其他组件中的接口值不会更改 到目前为止,我得到的是: import { Pipe, PipeTransform, EventEmitter } from '@angular/core'; import { GlobalService } from './global-servi
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;
}
}
从'@angular/core'导入{Pipe,PipeTransform,EventEmitter};
从“/global service”导入{GlobalService}
从“/my interface”导入{MyInterface}
@管道({name:'myPipe'})
导出类MyPipe实现PipeTransform{
私有值:字符串;
私有接口:MyInterface;
私有接口更改:EventEmitter;
构造函数(专用globalService:globalService){
此._接口=全球服务._接口;
this.interfaceChanged=this.globalService
.接口更改
.subscribe((新接口:MyInterface)=>{
此._接口=新接口;
});
}
转换(值:字符串,参数:任意[]):字符串{
for(此.language中的var键){
如果(键==值){
this.value=this._接口[键];
打破
}
}
返回此.value;
}
}
这里还有一个纯管道仅在值或参数更改时执行 可以将管道配置为不纯净管道,然后每次运行更改检测时都将执行该管道。但这可能会对性能产生严重影响
@Pipe({name: 'myPipe', pure: false})