假设我像这样创建一个管道:
import { Pipe, PipeTransform } from '@angular/core';
import { CurrencyPipe } from '@angular/common';
@Pipe({
name: 'awesomePipe'
})
export class AwesomePipe implements PipeTransform {
constructor(private currency: CurrencyPipe){}
transform(value: number) {
return this.currency
.transform(value.toString(), 'USD', true)
.substring(0, 3) + 'bajillion dollars';
}
}
然后将其注入具有以下内容的组件中:
import { AwesomePipe } from '../pipes/awesome.pipe';
......
......
export class Awesome {
awesomeness:[number] = [123123123123,32131231235,56465434565];
constructor(private awesome: AwesomePipe){}
}
并用于:
template: `
<div class="foo">
<custom-component
[displayValues]="awesomeness.map(awesome.transform)"
></custom-component>
</div>
`,
在转换函数中失败了:
VM8019:1 Uncaught ReferenceError: currency is not defined
在转换函数中添加调试器显示this
为未定义。
为什么?我该如何解决这个问题?