Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
Formcontrol error while getting dynamic name?

I am getting typeError here<p *ngIf="formValue.controls['{{obj.name}}'].invalid but when I hard code like this *ngIf="formValue.controls['uname'].invalid it works fine.

what is the problem here?

<span *ngFor="let obj of toolData">
    <span [ngSwitch]="obj.type">
        <span *ngSwitchCase="'text'">
            <label for="{{obj.name}}">{{obj.label}}</label>
            <input type="text" id="{{obj.name}}" name="{{obj.name}}" formControlName="{{obj.name}}" value="{{obj.value}}" required="{{obj.required}}" />
             <p *ngIf="formValue.controls['{{obj.name}}'].invalid && formValue.controls['{{obj.name}}'].touched" class="text-danger">Value required</p>
        </span>
     </span>
</span>

ToolData

toolData=[
    {"type":"text","name":"uname","label":"User Name","value":"user name","required":true},
    {"type":"number","name":"pval","label":"P val","value":"3","min":0,"max":100,"required":true},
]
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

ngIf evaluates the expression written inside that. You dont have to use string interpolation there. Use expression as below.

This jsfiddle will show a sample implementation.

It should be

*ngIf="formValue.controls[obj.name].invalid && formValue.controls[obj.name].touched"

So your template will be.

<span *ngFor="let obj of toolData">
    <span [ngSwitch]="obj.type">
        <span *ngSwitchCase="'text'">
            <label for="{{obj.name}}">{{obj.label}}</label>
            <input type="text" id="{{obj.name}}" name="{{obj.name}}" formControlName="{{obj.name}}" value="{{obj.value}}" required="{{obj.required}}" />
             <p *ngIf="formValue.controls[obj.name].invalid && formValue.controls[obj.name].touched" class="text-danger">Value required</p>
        </span>
     </span>
</span>
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!