我正在尝试从数字数组中过滤一个数字。但我得到了这个错误。这是我的代码。
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { NumberFilterPipe } from './number-filter.pipe';
import { FormsModule } from '@angular/forms';
import { StringFilterPipe } from './string-filter.pipe';
import { NumberFilterService } from './service/number-filter.service';
@NgModule({
declarations: [
AppComponent,
NumberFilterPipe,
StringFilterPipe
],
imports: [
BrowserModule,
FormsModule
],
providers: [
NumberFilterService],
bootstrap: [AppComponent]
})
export class AppModule { }
这是我的HTML
<input name= "searchtext" [(ngModel)]="searchtext">
{{searchtext}}
<li *ngFor = "let numbers of number | number-filter : searchtext ">{{numbers}}</li>
<br><br>
这个管道
import { Pipe, PipeTransform } from '@angular/core';
import { NumberFilterService } from './service/number-filter.service';
@Pipe({
name: 'number-filter'
})
export class NumberFilterPipe implements PipeTransform {
flterNumber: NumberFilterService;
transform(numbers: any, key: any): any[] {
return this.flterNumber.filterNumber(numbers, key);
}
}
这就是service.ts
import { Injectable } from '@angular/core';
@Injectable()
export class NumberFilterService {
arrayLen = 0;
itteration = 0;
result = [];
constructor() { }
filterNumber(values: any[], key: any): any[] {
this.arrayLen = values.length;
for (this.itteration = 0 ; this.itteration < this.arrayLen ; this.itteration ++ ) {
if (key === values[this.itteration]) {
this.result.push(values[this.itteration]);
}
}
return this.result;
}
}
我得到了这个错误。
"Uncaught Error: Template parse errors:
TypeError: Cannot read property 'toUpperCase' of undefined ("<input name= "searchtext" [(ngModel)]="searchtext">
{{searchtext}}
<li [ERROR ->]*ngFor = "let numbers of number | number-filter : searchtext ">{{numbers}}</li>
<br><br>"): ng:///AppModule/AppComponent.html@2:4
Parser Error: Unexpected token -, expected identifier, keyword, or string at column 31 in [let numbers of number | number-filter : searchtext ] in ng:///AppModule/AppComponent.html@2:4 ("l)]="searchtext">
{{searchtext}}
<li *ngFor = "let numbers of number | number-filter : searchtext ">[ERROR ->]{{numbers}}</li>
<br><br>"): ng:///AppModule/AppComponent.html@2:67
at syntaxError (webpack-internal:///../../../compiler/esm5/compiler.js:684)
at TemplateParser.parse (webpack-internal:///../../../compiler/esm5/compiler.js:24547)
at JitCompiler._parseTemplate (webpack-internal:///../../../compiler/esm5/compiler.js:33975)
at JitCompiler._compileTemplate (webpack-internal:///../../../compiler/esm5/compiler.js:33950)
at eval (webpack-internal:///../../../compiler/esm5/compiler.js:33852)
at Set.forEach (<anonymous>)
at JitCompiler._compileComponents (webpack-internal:///../../../compiler/esm5/compiler.js:33852)
at eval (webpack-internal:///../../../compiler/esm5/compiler.js:33722)
at Object.then (webpack-internal:///../../../compiler/esm5/compiler.js:673)
at JitCompiler._compileModuleAndComponents (webpack-internal:///../../../compiler/esm5/compiler.js:33721)"
我希望你有价值的解决方案。谢谢所有人。祝你有愉快的一天。
答案 0 :(得分:2)
以下问题: 在你的过滤器&#34;数字过滤器&#34;没有使用searchtext但你在html中使用。 这就是问题的尝试&#34;关键&#34;而不是&#34; searchtext&#34;在HTML中
<li *ngFor = "let numbers of number | number-filter : searchtext ">{{numbers}}</li>
to
<li *ngFor = "let number of numbers | number-filter : key">{{numbers}}</li>
答案 1 :(得分:1)
我找到了这种工作方式
Pipe.ts
STARTING MEMORY MANAGEMENT TESTING
******************* Creating DATA *******************
[New] 4
[New] 4
组件模板
$display_box_content = str_replace('\'', "\"",, $display_box_content);
组件ts
import { Pipe, PipeTransform } from "@angular/core";
@Pipe({
name: 'myfilter'
})
export class MyFilter implements PipeTransform {
transform(items: any[], term: any[]): any {
if (!term)
return items;
return items.filter(item => item == term);
}
}
正在运行的最终更新
以下是经过全面测试和运作的
html模板文件
<input type="text" [(ngModel)]="term" placeholder="filter by prefix" />
<li *ngFor="let product of listA | myfilter:term" >{{product}}</li>
pipe.ts文件
val:number ;
listA = [1,20,11,99];
过滤SErvice文件
<input name="searchtext" [(ngModel)]="searchtext" (ngModelChange)="fitlerNumbers()" > {{numbers|json}}
<ul>
<li *ngFor="let number of numbers|numberfilter:2">{{number}}</li>
</ul>
更新到此结束