0
Good night.
I created a Component for a standardized template and I’m having trouble adjusting the name of the radio input making only one selected and not all.
theme-thumbnail.component.ts
@Component({
selector: 'tema-thumbnail',
templateUrl: './tema-thumbnail.component.html'
})
export class TemaThumbnailComponent implements OnInit {
@Input() tema: Tema
@Input() parent: FormGroup
@Input() fcName: string
constructor() { }
ngOnInit() {
}
}
theme-thumbnail.component.html
<div class="col-xs-6 col-sm-4 col-md-2 col-lg-2" [formGroup]="parent">
<label>
<input type="radio" [formControlName]="fcName" [value]="tema.id" >
<figure>
<img src="{{tema.url}}" alt="{{tema.titulo}}">
<figcaption>
<h4>{{tema.titulo}}</h4>
<p>{{tema.descricao}}</p>
</figcaption>
</figure>
</label>
</div>
And I’m using it like this:
event-information.component.ts
export class EventoInformacoesComponent extends Form implements OnInit {
temas: Tema[]
...
...
ngOnInit() {
this.temas = this.eventoService.obterTemasDisponiveis()
}
...
...
this.formBuilder.group({
...
'codigo_template': this.formBuilder.control('',Validators.required])
})
...
...
}
event-information.component.html
<form role="form" [formGroup]="form">
...
...
<div class="row radio">
<tema-thumbnail *ngFor="let tema of temas" [tema]="tema" [parent]="form" [fcName]="'codigo_template'"></tema-thumbnail>
</div>
...
...
</form>
However I am not able to make the radio input select only one option. This being possible to select several options.
Generated input is without "name". Contains only "ng-reflect-name"
<input type="radio" ng-reflect-form-control-name="codigo_template" ng-reflect-value="1" ng-reflect-name="codigo_template" class="ng-dirty ng-valid ng-touched">
pq ta without the name???
– Eduardo Vargas
Putting the name (in the same template for testing. In theme-thumbnail.component.html <input type="radio" [formControlName]="fcName" [value]="theme.id" name="optTema"> ) get error. ERROR Error: If you define Both a name and a formControlName attribute on your radio button, their values must match. Ex: <input type="radio" formControlName="food" name="food">
– Pickles Dog
pq vc do not arrow the name with the same value as formControl as the error message suggests?
– Eduardo Vargas
I tried but I might be doing something wrong. I tried so: <input type="radio" [formControlName]="fcName" [value]="theme.id" name="fcName"> and get the same mistake. I tried so: <input type="radio" [formControlName]="fcName" [value]="tema.id" [name]="fcName"> and failed. But still the same problem. I select several and not just one.
– Pickles Dog
Continuing.... And I tried so: <input type="radio" [formControlName]="fcName" [value]="tema.id" [name]="name"> where "name" is a @input() name and in the call do *<thumbnail theme ngFor="Let theme of themes" [theme]="theme" [Parent]="form" [fcName]="'codigo_template'" [name]="'codigo_template'"></theme-thumbnail>. I do not get an error but select several and not just one. Same for *<thumbnail theme ngFor="Let tema of temas" [theme]="tema" [Parent]="form" [fcName]="'codigo_template'" [name]="codigo_template"></theme-thumbnail> without ' ' in the name.
– Pickles Dog