2017-05-26 50 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