2

I encountered this piece of code [MaxLength]="this['point']"in angular template.
I don't know how to find out the meaning online.
I know the meaning of [property]="fieldOnComponent".
But I'm wondering why using this and [''] in the code.
What's the syntax associated with this?

niaomingjian
  • 3,472
  • 8
  • 43
  • 78

2 Answers2

1

It is undocumented feature that let you write code in template like

[x]="this.prop"
{{ this.prop }}

or using brackets notation

[x]="this['prop']"
{{ this['prop' ]}}

where this is component instance.

Plunker Example

Support for this was added in angular 2.0.0-rc.5.

yurzui
  • 205,937
  • 32
  • 433
  • 399
0

The notation

this['point']

is in Javascript (and thus, in Typescript, used in Angular, and also available in Angular templates) the same as

this.point

You can get more info about the subtle difference of the 2 notations here for example : JavaScript property access: dot notation vs. brackets?


this refers to the component. Usually, you don't have to specify it in a template, and you should just write point instead of this.point . But if you want to use the ['point'] (bracket notation) instead of the dot notation, you have to specify this in this case.

Pac0
  • 21,465
  • 8
  • 65
  • 74