Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

259
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda