【发布时间】:2016-10-16 03:00:25
【问题描述】:
尝试编写自定义管道来隐藏一些项目。
import { Pipe } from '@angular/core';
// Tell Angular2 we're creating a Pipe with TypeScript decorators
@Pipe({
name: 'showfilter'
})
export class ShowPipe {
transform(value) {
return value.filter(item => {
return item.visible == true;
});
}
}
HTML
<flights *ngFor="let item of items | showfilter">
</flights>
组件
import { ShowPipe } from '../pipes/show.pipe';
@Component({
selector: 'results',
templateUrl: 'app/templates/results.html',
pipes: [PaginatePipe, ShowPipe]
})
我的物品有可见的属性,可以是真也可以是假。
但是没有显示,我的管道有问题吗?
我认为我的管道正在工作,因为当我将管道代码更改为:
import { Pipe } from '@angular/core';
// Tell Angular2 we're creating a Pipe with TypeScript decorators
@Pipe({
name: 'showfilter'
})
export class ShowPipe {
transform(value) {
return value;
}
}
它将显示所有项目。
谢谢
【问题讨论】:
-
您是否将
pipes: [ShowPipe]添加到您正在使用管道的组件中?我看不出你的代码有什么问题。 -
如果你把它变成不纯的管道会发生什么?
@Pipe({ name: 'showfilter', pure : false }) -
嘿,我刚刚对您的代码进行了快速测试,没有任何问题(除了不遵循推荐的做法:P)。您能否检查您的任何项目是否具有“可见”=== true?
-
“不遵循推荐的做法”是什么意思?有什么具体的提示吗?
标签: javascript angular angular-pipe