Angular在测试版中有很多变化,我的问题是尝试在ngFor中使用管道和索引,我收到此消息:
分析器错误:意外的令牌=和
The pipe 'let' could not be found
当我使用这段代码时:
<div style="overflow-y: scroll; max-height: 200px;"> <div (click)="showComentario(index);" *ngFor="let comment of comentarios| filterSource:selectedSource | let index=index; "> {{comment.comment}} </div> </div>
如果我改变这样的顺序:
<div style="overflow-y: scroll; max-height: 200px;"> <div (click)="showComentario(index);" *ngFor="let comment of comentarios;let index=index;| filterSource:selectedSource | "> {{comment.comment}} </div> </div>
我收到这条消息:
Template parse errors: TypeError: key[0] is undefined Parser Error: Unexpected token |,expected identifier,keyword,or string at column 47 in [let comment of comentarios; let index=index;
我如何同时使用管道和索引?
<div style="overflow-y: scroll; max-height: 200px;"> <div (click)="showComentario(index);" *ngFor="let comment of comentarios | filterSource:selectedSource;let index=index "> {{comment.comment}} </div> </div>
我一直收到这些错误:
TypeError:key [0]未定义,Parser Error:意外的令牌|
解决方法
试试下面,
<div (click)="showComentario(i)" *ngFor="let comment of comentarios | filterSource : selectedSource; index as i" > {{comment.comment}} </div>
希望这可以帮助!!