2017-05-26 51 views
0

IHAVE一个问题,我想她的类别进行过滤我的组件的列表,但我管不工作,你能不能帮我管角2过滤器组件通过枚举

,这是我的烟斗:

import { Pipe, PipeTransform } from '@angular/core'; 
 

 
@Pipe({ 
 
    name: 'matchCategory' 
 
}) 
 
export class MatchesCategoryPipe implements PipeTransform { 
 
    transform(items: Array<any>, category: string): Array<any> { 
 
    return items.filter(item => item.category === category); 
 
    } 
 
} 
 

 
<!-- list of category!--> 
 

 

 
export enum ComponentCategory { 
 
    FormControls = <any> 'Form Controls', 
 
    Containers = <any> 'Containers' , 
 
    Boxes = <any> 'Boxes', 
 
    DataPresentation = <any> 'Data Presentation', 
 
    Layout = <any> 'Layout', 
 
    Miscellaneous = <any> 'Miscellaneous', 
 
    All = <any> 'All' 
 
}
<tr *ngFor="let c of componentDescriptorsList | matchCategory: c.category" [ngValue]="'Form Controls'"> 
 
     <!--<tr *ngFor="let c of componentDescriptorsList">--> 
 
     <td><a href="#/components/{{c.selector}}">{{c.title}}</a></td> 
 
     <td>&#60;{{c.selector}}&#62;</td> 
 
     <td>{{c.description}}</td> 
 
     <td>{{c.category}}</td> 
 
     
 

我的组件列表中有一类,我想以显示为例只是分量谁categoryis“表单控件”

感谢

+0

管道基本的例子有你注册的管道中app.module.ts –

+0

没有我不但现在我已经写在app.module中,并且我有一个新的错误 – ouanounou

+0

我发布错误日志anwser – ouanounou

回答

0

import { Pipe, PipeTransform } from '@angular/core'; 
 

 
@Pipe({ 
 
    name: 'matchesCategory' 
 
}) 
 
export class MathcesCategoryPipe implements PipeTransform { 
 
    transform(items: Array<any>, category: string): Array<any> { 
 
     return items.filter(item => item.category === category); 
 
    } 
 
}
<li *ngFor="let model; of models | matchesCategory:model.category" (click)="gotoDetail(model)"> 
 
<select (change)="selectedCategory = $event.target.value"> 
 
    <option *ngFor="let model of models ">{{model.category}}</option> 
 
</select>

使用angular2

0

pipe category 
 

 

 
import { Pipe, PipeTransform } from '@angular/core'; 
 

 
@Pipe({ 
 
    name: 'matchCategory' 
 
}) 
 
export class MatchesCategoryPipe implements PipeTransform { 
 
    transform(items: Array<any>, category: string): Array<any> { 
 
    return items.filter(item => item.category === category); 
 
    } 
 
}
html file to display component & category 
 

 
<tr *ngFor="let c of componentDescriptorsList | matchCategory: c.category" > 
 
     <!--<tr *ngFor="let c of componentDescriptorsList">--> 
 
     <td><a href="#/components/{{c.selector}}">{{c.title}}</a></td> 
 
     <td>&#60;{{c.selector}}&#62;</td> 
 
     <td>{{c.description}}</td> 
 
     <td>{{c.category}}</td> 
 
     </tr>
typeScript file 
 

 
import { TypeDecorator } from '@angular/core' 
 
import { 
 
    makeDecorator, 
 
    makePropDecorator 
 
} from './decorators' 
 

 
export enum ComponentCategory { 
 
    FormControls = <any> 'Form Controls', 
 
    Containers = <any> 'Containers' , 
 
    Boxes = <any> 'Boxes', 
 
    DataPresentation = <any> 'Data Presentation', 
 
    Layout = <any> 'Layout', 
 
    Miscellaneous = <any> 'Miscellaneous', 
 
    All = <any> 'All' 
 
} 
 

 
export interface ComponentDescriptorDecorator { 
 
    (obj: ComponentDescriptor): TypeDecorator 
 
    new (obj: ComponentDescriptor): ComponentDescriptor 
 
} 
 

 
export interface Descriptor { 
 
    title: string 
 
    description: string 
 
} 
 

 
export interface ComponentDescriptor extends Descriptor { 
 
    category: ComponentCategory 
 
    creationDate: Date 
 
    updatedDate?: Date 
 
    relatedComponents?: any[] 
 
    tags?: string[] 
 
    deprecated?: boolean 
 
}

+0

你现在得到的错误是什么 –

+0

无法读取undefined的属性'category' TypeError :无法在Object.eval [as updateDirectives] – ouanounou

+0

上读取未定义的 的属性'category'它很好,我忘了在管道中插入参数 – ouanounou