angular2管道模板解析错误
angular2 pipe Template parse error
我正在尝试使用管道实现简单的搜索。我对 pipe 和 angular2 的概念真的很陌生,所以我需要你的帮助
这是我的 html:
<input type="textarea" placeholder="branch" [(ngModel)]="search.branch" name="branch">
<tr *ngFor="let loan of loans | loanfilter:search.branch ">
<td><input type="checkbox"></td>
<td>{{loan.loanId}}</td>
<td>{{loan.custName}}</td>
<td>{{loan.vehicleNum}}</td>
<td>{{loan.TC}}</td>
<td>{{loan.RE}}</td>
</tr>
loan.component.ts:
import { Component } from '@angular/core';
import { LoanService } from '../../services/loans.service';
import { Loan } from '../../../loan';
import { Injectable, Pipe, PipeTransform } from '@angular/core';
import { LoanFilterPipe } from '../../pipes/loan.pipe';
@Component({
moduleId: module.id,
selector: 'loans',
templateUrl: './loans.component.html',
providers: [LoanService],
declarations: [LoanFilterPipe] //getting error on this line: Object literal may only specify known properties, and 'declarations' does not exist in type 'Component'.
})
export class LoansComponent{
loans : Loan[];
custName: any;
constructor(private loanService: LoanService){
this.getloans();
};
getloans(){
this.loanService.getLoan()
.subscribe(loans => {
this.loans = loans;
console.log(loans);
});
}
};
我得到的错误是:
Uncaught (in promise): Error: Template parse errors: Can't bind to
'ngModel' since it isn't a known property of 'input'
loan.pipe.ts:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'loanfilter'
})
export class LoanFilterPipe implements PipeTransform{
transform(loans: any[], args: any): any{
return loans.filter(item => item.title.indexOf(args[0].title) !== -1);
}
}
我还使用了 module.ts 文件来声明管道和所有其他组件:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { LoansComponent } from './components/loans/loans.component';
import { LoanFilterPipe } from './pipes/loan.pipe';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule, HttpModule ],
declarations: [ AppComponent, LoansComponent, LoanFilterPipe ],
bootstrap: [ AppComponent ],
exports: [LoanFilterPipe]
})
export class AppModule { }
在您的应用程序模块中导入 FormsModule
。
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { LoansComponent } from './components/loans/loans.component';
import { LoanFilterPipe } from './pipes/loan.pipe';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule, HttpModule, FormsModule ],
declarations: [ AppComponent, LoansComponent, LoanFilterPipe ],
bootstrap: [ AppComponent ],
exports: [LoanFilterPipe]
})
export class AppModule { }
您的代码中缺少 FormsModule
because according to angular FormsModule
is must to add while bootstraping if you are using ngModel
because as of now ngModel is a part of FormModule
正如其他答案所建议的那样,您必须导入 FormsModule
此外,您已将 search.branch
设置为组件打字稿文件中未声明的 ngModel。
而且您不必在组件装饰器中提供 declarations
数组。
从 @Component.
中删除 declarations: [LoanFilterPipe]
它在 NgModule.You 中给出,除非您要在另一个模块中使用,否则不必将其放在导出中。
我正在尝试使用管道实现简单的搜索。我对 pipe 和 angular2 的概念真的很陌生,所以我需要你的帮助
这是我的 html:
<input type="textarea" placeholder="branch" [(ngModel)]="search.branch" name="branch">
<tr *ngFor="let loan of loans | loanfilter:search.branch ">
<td><input type="checkbox"></td>
<td>{{loan.loanId}}</td>
<td>{{loan.custName}}</td>
<td>{{loan.vehicleNum}}</td>
<td>{{loan.TC}}</td>
<td>{{loan.RE}}</td>
</tr>
loan.component.ts:
import { Component } from '@angular/core';
import { LoanService } from '../../services/loans.service';
import { Loan } from '../../../loan';
import { Injectable, Pipe, PipeTransform } from '@angular/core';
import { LoanFilterPipe } from '../../pipes/loan.pipe';
@Component({
moduleId: module.id,
selector: 'loans',
templateUrl: './loans.component.html',
providers: [LoanService],
declarations: [LoanFilterPipe] //getting error on this line: Object literal may only specify known properties, and 'declarations' does not exist in type 'Component'.
})
export class LoansComponent{
loans : Loan[];
custName: any;
constructor(private loanService: LoanService){
this.getloans();
};
getloans(){
this.loanService.getLoan()
.subscribe(loans => {
this.loans = loans;
console.log(loans);
});
}
};
我得到的错误是:
Uncaught (in promise): Error: Template parse errors: Can't bind to 'ngModel' since it isn't a known property of 'input'
loan.pipe.ts:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'loanfilter'
})
export class LoanFilterPipe implements PipeTransform{
transform(loans: any[], args: any): any{
return loans.filter(item => item.title.indexOf(args[0].title) !== -1);
}
}
我还使用了 module.ts 文件来声明管道和所有其他组件:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { LoansComponent } from './components/loans/loans.component';
import { LoanFilterPipe } from './pipes/loan.pipe';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule, HttpModule ],
declarations: [ AppComponent, LoansComponent, LoanFilterPipe ],
bootstrap: [ AppComponent ],
exports: [LoanFilterPipe]
})
export class AppModule { }
在您的应用程序模块中导入 FormsModule
。
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { LoansComponent } from './components/loans/loans.component';
import { LoanFilterPipe } from './pipes/loan.pipe';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule, HttpModule, FormsModule ],
declarations: [ AppComponent, LoansComponent, LoanFilterPipe ],
bootstrap: [ AppComponent ],
exports: [LoanFilterPipe]
})
export class AppModule { }
您的代码中缺少 FormsModule
because according to angular
FormsModule
is must to add while bootstraping if you are usingngModel
because as of now ngModel is a part of FormModule
正如其他答案所建议的那样,您必须导入 FormsModule
此外,您已将 search.branch
设置为组件打字稿文件中未声明的 ngModel。
而且您不必在组件装饰器中提供 declarations
数组。
从 @Component.
declarations: [LoanFilterPipe]
它在 NgModule.You 中给出,除非您要在另一个模块中使用,否则不必将其放在导出中。