【问题标题】:angular2 pipe Template parse errorangular2管道模板解析错误
【发布时间】:2017-05-27 09:42:42
【问题描述】:

我正在尝试使用管道实现简单的搜索。我对管道和 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);
            });

    }
};

我得到的错误是:

未捕获(承诺中):错误:模板解析错误:无法绑定到 'ngModel' 因为它不是 '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 { }

【问题讨论】:

    标签: angular filter angularjs-filter pipes-filters angular2-custom-pipes


    【解决方案1】:

    正如其他答案所建议的,您必须导入 FormsModule

    您还设置了search.branch 为未在组件打字稿文件中声明的ngModel。

    而且您不必在组件装饰器中提供declarations 数组。 从@Component 中删除declarations: [LoanFilterPipe]

    它在 NgModule 中给出。除非你要在另一个模块中使用,否则不必将它放在导出中。

    【讨论】:

    • 是我的 LoanFilterPipe 错误,因为我收到此错误Cannot read property 'filter' of undefined
    • 不,我认为这与异步发布请求有关。将您的 ngFor 放入 &lt;div *ngIf="loans"&gt;
    • 或在组件构造函数中设置this.loans = [];
    • 天哪!!1 这些错误不断出现Cannot read property 'indexOf' of undefined
    • 是不是因为每次迭代都会覆盖我的数组
    【解决方案2】:

    您的代码中缺少FormsModule

    因为根据角度FormsModule,如果您使用ngModel,则必须在引导时添加,因为到目前为止,ngModel 是 FormModule 的一部分

    【讨论】:

    • 现在遇到不同的错误TypeError: Cannot read property 'branch' of undefined
    • 现在收到此错误:Cannot read property 'indexOf' of undefined
    【解决方案3】:

    在您的应用模块中导入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 { }
    

    【讨论】:

    • 现在出现不同的错误TypeError: Cannot read property 'branch' of undefined
    • 你需要在你的LoansComponent中定义public search:any={ branch:''}
    • 现在收到此错误:Cannot read property 'filter' of undefined
    • 尝试将if(!loans || !args) return;作为第一行插入transform()函数内LoanFilterPipe
    • 收到此错误:Cannot read property 'indexOf' of undefined
    猜你喜欢
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 2017-05-01
    • 2017-01-31
    • 2019-04-25
    • 2019-10-03
    相关资源
    最近更新 更多