nimgwfc
nimgwfc

Reputation: 1509

Angular use variable value as opposed to string of variable name

I am working with an array that looks something like this:

[
   {
     f_name: "c_name",
     title: "k c name",
     con_name: "cname",
     path: "this.dialogData.name"
   },
   {
     f_name: "c_num",
     title: "k c num",
     con_name: "cnum",
     path: "this.dialogData.number"
   }
]

In the html file, I'm looping through this array as follows:

        <mat-form-field *ngFor ="let f of fields">
            <mat-label>{{f.title}}</mat-label>
            <input  formControlName={{f.con_name}} matInput name={{f.con_name}} value={{f.path}}>
        </mat-form-field>

Everything is working as expected except the {{f.path}} is coming up as the string as opposed to the variable.

So for example, in the first question, the value is set to this.dialogData.name as opposed to test_name which is the value that corresponds to the variable this.dialogData.name.

Is there anything I can do to refer to the underlying variable? As opposed to having the value as the path string.

Upvotes: 0

Views: 639

Answers (1)

Barremian
Barremian

Reputation: 31125

Expanding from my comment, I believe you actually tried to initialize the properties path in each element to the properties in this.dialogData instead of assigning the strings. In that case, you could remove the enclosing quotations for the path properties.

Try the following

fields = [
  {
    f_name: "c_name",
    title: "k c name",
    con_name: "cname",
    path: this.dialogData.name         // <-- no quotations
  },
  {
    f_name: "c_num",
    title: "k c num",
    con_name: "cnum",
    path: this.dialogData.number       // <-- no quotations
  }
];

Upvotes: 1

Related Questions