简体   繁体   English

Angular 2 ngIf和ngFor

[英]Angular 2 ngIf and ngFor

In Angular 1, the ngIf and ngRepeat directives would create their own scope and you had to be careful when accessing scope variable inside these. 在Angular 1中,ngIf和ngRepeat指令将创建它们自己的作用域,并且在其中访问作用域变量时必须小心。

Are there any similar gotcha's in Angular 2 using *ngIf or *ngFor? 使用* ngIf或* ngFor在Angular 2中是否有类似的陷阱?

No, because there are no scopes in Angular 2, thankfully. 不会,因为Angular 2中没有作用域。 In Angular 2, the context for NgIf and NgFor is simply the component. 在Angular 2中,NgIf和NgFor的上下文只是组件。

The only possible gotcha I've seen is described here: Angular2, *ngIf and local template variables 我在这里看到的唯一可能的陷阱描述如下: Angular2,* ngIf和局部模板变量

So, if you define a local template variable inside NgFor, note that it can only be used inside the template block (ie, sibling and/or child elements inside the template block). 因此,如果在NgFor中定义本地模板变量,请注意,它只能在模板块内使用(即,模板块内的同级和/或子元素)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM