Angular 2 ng for first,last,index loop [英] Angular 2 ngfor first, last, index loop

查看:92
本文介绍了Angular 2 ng for first,last,index loop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例中,我尝试将默认值设置为默认值: plunkr

I'm trying to set as default the first occurrence in this example: plunkr

出现以下错误:

Unhandled Promise rejection: Template parse errors:
TypeError: Cannot read property 'toUpperCase' of undefined ("dButtonToggleGroup">
                                                            <md-button-toggle [ERROR ->]*ngFor="let indicador of indicadores; #first = first" value="indicador.id" [checked]="first">
      "): ng:///AppModule/HomeComponent.html@35:78
Parser Error: Unexpected token #, expected identifier, keyword, or string at column 31 in [let indicador of indicadores; #first = first] in ng:///AppModule/HomeComponent.html@35:78 ("       <md-button-toggle *ngFor="let indicador of indicadores; #first = first" value="indicador.id" [ERROR ->][checked]="first">
                                                                <span>{{ indicado"): ng:///AppModule/HomeComponent.html@35:153

怎么了?

推荐答案

查看此 plunkr .

绑定变量时,需要使用方括号.另外,当您要获取对html中元素的引用时,可以使用井号标签,而不是在这样的模板内部声明变量.

When you're binding to variables, you need to use the brackets. Also, you use the hashtag when you want to get references to elements in your html, not for declaring variables inside of templates like that.

<md-button-toggle *ngFor="let indicador of indicadores; let first = first;" [value]="indicador.id" [checked]="first"> 
...


修改: 感谢 Christopher Moore : Angular公开了以下局部变量:


Thanks to Christopher Moore: Angular exposes the following local variables:

  • index
  • first
  • last
  • even
  • odd
  • index
  • first
  • last
  • even
  • odd

这篇关于Angular 2 ng for first,last,index loop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆