0

I understand that I can assign local template variables to hold element reference like following

<input #myInput />

But can I declare a variable with custom value?

For example:

<!-- Somewhere I do #obj = model.data.something -->

<input [(ngModel)]="obj.inputA" >
<input [(ngModel)]="obj.inputB" >

Instead of doing

<input [(ngModel)]="model.data.something.inputA" >
<input [(ngModel)]="model.data.something.inputB" >

1 Answer 1

2

You can't declare a variable with a custom value.

A template variable

  • can refer to the element or component it was applied to
  • can refer to a specific directive (if one or more are applied to an element) by referring to its exportAs: 'xxx' property #myVar="xxx"
  • can be declared by structural directives like *ngFor="let x of y; let i=index"

There is an open issue to support assigning custom expressions as well https://github.com/angular/angular/issues/2451

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.