Angular 2 ngfor第一个,最后一个,索引循环 [英] Angular 2 ngfor first, last, index loop

查看:33
本文介绍了Angular 2 ngfor第一个,最后一个,索引循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将本示例中的第一次出现设置为默认值: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"> 


感谢克里斯托弗·摩尔:Angular 公开了以下局部变量:


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

  • 索引
  • 第一
  • 最后
  • 偶数
  • 奇数

这篇关于Angular 2 ngfor第一个,最后一个,索引循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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