问题
Let's say we have a prop
variable in the component class and we use it via interpolation in the template (stackblitz demo):
component class:
@Component({...})
export class AppComponent {
prop = 'Test';
...
}
template:
<p>{{ this.prop }}</p>
<p>{{ prop }}</p>
Why in Angular it's possible to use this
keyword in templates without any warnings/error (even in AOT mode)? What's behind it?
Edit
According to the remark in the answer: this
refers to the component itself for which the template was rendered. But I can also create a template variable and access to it using this
:
<input #inp> {{ this.inp.value }}
In this case we don't have an inp
variable in the component class and I still get the access to it using {{this.inp...}}
. Magic?
回答1:
I don't think somebody can give a very much exact answer here (maybe somebody from Angular CLI team), however the outcome I came to is that the component renderer fully ignores this
keyword in the places where it seems valid (with some exceptions).
Proof
<input #heroInput value="0">
This prints the component JSON without heroInput: {{ this | json }}
<input #heroInput value="0">
This prints 0: {{ this.heroInput.value }}
<div *ngFor="let val of [1,2,3]">
<input #heroInput [value]="val">
Overrides heroInput with current value: {{ this.heroInput.value }}
</div>
This prints 0: {{ this.heroInput.value }}
One can assume from the above that this
is similar to AngularJS (angular 1) scope
, where the scope
contains the component properties.
It does not explain why heroInput is not listed in this | json
still.
However the following is totally broken:
{{ this['heroInput'].value }}
It gives an error: cannot get value
of undefined. It should, not, it must work, unless (the only explanation) this
is just ignored in every case but
{{ this | json }}
where it refers to the component, because this is the only way to debug the whole component object from the template. Maybe there are some other exceptions, still.
Updated stackblitz
回答2:
this
refers to the component itself for which the template was rendered. On the template you can access only members
of the component. This means that this
is implicitly added to each property which you use in the template.
This two accesses are the same - the 2nd one implicitly use this
in front of it.
<p>{{ this.prop }}</p>
<p>{{ prop }}</p>
The same is when you use this
in the component. When you want to access prop
in the component you need to prefix it with this.prop
to inform that you are accessing property
of the component, not a local variable.
回答3:
I felt we can't get a proper explanation on this but,
- I went through a case where i will be creating the members of the component dynamically.
In here it might go wrong, if i don't use this.member (In my case it was actually
this[member]
).Create a member in component like,
this.members(prop=> this[prop]={})
- Usage in template will be like,
{{this[prop]}}
will give expected result.{{prop}}
will not give expected result because it will print value of list.
来源:https://stackoverflow.com/questions/52152290/using-this-keyword-in-angular-components-template