* ngFor:#vs let的含义? [英] *ngFor: meaning of # vs let?

查看:63
本文介绍了* ngFor:#vs let的含义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Am以angular2开头,遇到了两种不同的语法,但不确定它们的区别-或更确切地说它们是如何工作的.

Am starting out in angular2 and have run into 2 different syntax but am not sure of the difference - or rather how they work.

我看到了:

 <select class="form-control" required
      [(ngModel)]="model.power"
        ngControl="power" #power="ngForm" >
      <option *ngFor="let p of powers" [value]="p">{{p}}</option>
    </select>

但这也可行

<div *ngFor="#game of games" (click)="gotoGame(game)" class="col-1-4">
        <span>{{game.id}}</span>{{game.name}}
        <br> {{game.description}}
        <br> {{game.genre.name}}
  </div>

是另一个的别名吗?一个人比另一个人有什么优势?

is one just an alias of the other? what are the advantages of one over the other?

推荐答案

在beta.17中,语法更改了,只有这种形式才有效

In beta.17 the syntax changed and only this form is valid anymore

<select class="form-control" required
  [(ngModel)]="model.power"
    ngControl="power" #power="ngForm" >
  <option *ngFor="let p of powers" [value]="p">{{p}}</option>
</select>

另请参见 https://github.com/angular/angular/blob/master/CHANGELOG.md#200-beta17-2016-04-28

更改BREAK

引用#...现在始终表示ref-.

BREAKING CHANGES

The reference #... now always means ref-.

之前:

  • ngFor之外,#...表示引用.
  • ngFor内,它表示局部变量.
  • Outside of ngFor, a #... meant a reference.
  • Inside of ngFor, it meant a local variable.

此模式令人困惑.

之后:

  • <template #abc>现在定义了对TemplateRef的引用,而不是模板内部使用的输入变量.
  • 在声明局部变量的结构指令(例如*ngFor)中,不赞成使用#....请使用let.
  • <div *ngFor="#item of items">现在变为<div *ngFor="let item of items">
  • var-...已过时.
  • 使用#*ngFor之外的ref-
  • 对于ngFor,请使用以下语法:<template ngFor let-... [ngForOf]="...">
  • <template #abc> now defines a reference to a TemplateRef, instead of an input variable used inside of the template.
  • Inside of structural directives that declare local variables, such as *ngFor, usage of #... is deprecated. Use let instead.
  • <div *ngFor="#item of items"> now becomes <div *ngFor="let item of items">
  • var-... is deprecated.
  • use # or a ref- outside of *ngFor
  • for ngFor, use the syntax: <template ngFor let-... [ngForOf]="...">

这篇关于* ngFor:#vs let的含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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