1

I'm learning Angular 2 recently. And I can't understand the meaning of "*" in *ngFor. Could anyone explain it for me?

Thank you!

ZhangXu
  • 131
  • 10

1 Answers1

3

When we reviewed the NgFor and NgIf built-in directives, we called out an oddity of the syntax: the asterisk (*) that appears before the directive name.

The * is a bit of syntactic sugar that makes it easier to read and write directives that modify HTML layout with the help of templates. NgFor, NgIf, and NgSwitch all add and remove element subtrees that are wrapped in tags.

Meaning

ngFor can only be applied to a <template> elment.

*ngFor can be applied to any element. When used the <template> element is created behind the scene.

daan.desmedt
  • 3,752
  • 1
  • 19
  • 33