在ngFor中使用管道和索引的角度

前端之家收集整理的这篇文章主要介绍了在ngFor中使用管道和索引的角度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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>

希望这可以帮助!!

猜你在找的Angularjs相关文章