我试图过滤数据并将其呈现在htmltable中。 下面我的代码: 在module.ts在@Pipe中过滤数据错误
import Register10Pipe = require("./register10/register10.pipe");
@NgModule({
declarations: [
Register10Pipe.FilterPipe
])
在my.pipe.ts
import { Component, OnInit, Pipe, PipeTransform, Injectable } from '@angular/core';
@Pipe({
name: 'sectionGroup'
})
export class FilterPipe implements PipeTransform {
transform(items: any[], field: number, value: number): any[] {
if (!items) return [];
return items.filter(it => (+it[field]) === (+value));
}
}
在my.component.ts
import { FilterPipe } from './register10.pipe';
export class MyComponent implements OnInit {
filter: FilterPipe;
......
HTML,其中的数据必须呈现
<ng-container *ngFor='let data1 of sectionList'>
<tr>
<td colspan="7">
{{data1.name}}
<button (click)="ArrRow($event)" style="float: right" id="{{data1.id}}">+</button>
</td>
</tr>
<tr *ngFor="let data2 of (model.Register10Data | filter:'sectionGroup':data1.id); let dataIndex = index">
<td>
{{dataIndex}}
</td>
</tr>
</ng-container>
sectionList它是像myObj这样的对象数组{number:id,string:name} 我从此数组中获取id并尝试过滤从服务器接收的数据。正如我所理解的在角度可以使用@Pipe。我知道还有其他的方法,但是我更喜欢这个。但我得到例外。
Unhandled Promise rejection: Template parse errors: The pipe 'filter' could not be found (" ]*ngFor="let data2 of (model.Register10Data | filter:'sectionGroup':data1.id); let dataIndex = index">"): [email protected]:20 ; Zone: ; Task: Promise.then ; Value: Error: Template parse errors: The pipe 'filter' could not be found (" ]*ngFor="let data2 of (model.Register10Data | filter:'sectionGroup':data1.id); let dataIndex = index">"): [email protected]:20
您的自定义管什么管中的'+'在做什么? –
@Christopher。它将值转换为一个数字(我假设) – AngularChef
ok - 你在两个嵌套的for循环中声明了'data',这可能不会帮助调试的提示 –