angular2的* ngFor中的*是什么意思? [英] What is the meaning of * in *ngFor in angular2?

查看:194
本文介绍了angular2的* ngFor中的*是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例中ngFor之前的*的含义是什么?

What is the meaning of * before ngFor in following sample and why it is needed?

<div *ngFor="#hero of heroes" (click)="selectHero(hero)">
  {{hero.name}}
</div>

推荐答案

ngFor仅可应用于<template>. *ngFor是可以应用于任何元素的缩写,并且<template>元素是在幕后隐式创建的.

ngFor can only be applied to a <template>. *ngFor is the short form that can be applied to any element and the <template> element is created implicitly behind the scene.

https://angular.io/api/common/NgForOf

语法

  • <li *ngFor="let item of items; let i = index">...</li>
  • <li template="ngFor let item of items; let i = index">...</li>
  • <template ngFor let-item [ngForOf]="items" let-i="index"><li>...</li>
  • <li *ngFor="let item of items; let i = index">...</li>
  • <li template="ngFor let item of items; let i = index">...</li>
  • <template ngFor let-item [ngForOf]="items" let-i="index"><li>...</li>

所有结构指令

> 柱塞示例

更新

在2.0.0最终版本<ng-container>中引入了该功能,其行为类似于<template>(实际上未添加到DOM中的包装元素),但支持*ngFor="..."语法.

With the 2.0.0 final release <ng-container> was introduced, that behaves like <template> (a wrapper element that isn't actually added to the DOM) but supports the *ngFor="..." syntax.

这篇关于angular2的* ngFor中的*是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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