Name of input type radio with formControl

Asked

Viewed 420 times

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???

  • 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">

  • pq vc do not arrow the name with the same value as formControl as the error message suggests?

  • 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.

  • 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.

1 answer

0

Look, the radio button needs to have a name equal in all radio Buttons so that only one of them is selectable. Then, try to use the nameinstead of ng-reflect-form-control-name.

Example:

<input (change)="onSelectMovimentacao(1)" class="form-check-inline ml-2" type="radio" name="movimentacao"> Entrada
<input (change)="onSelectMovimentacao(2)" class="form-check-inline ml-2" type="radio" name="movimentacao" [checked]="true"> Saída

I could not get the value of the radio button selected by formControlName, in the example above, as it is only an option, I used the (change) to send the value. To get the value, I used the document in the component, when I use a ngFor.

const auxPerm:any = document.querySelectorAll('input[class="form-check-inline ml-2 perm"]:checked');

Anything, explain more in the comments.

  • When I put a name I get error. I named the <input type="radio" component template [formControlName]="fcName" [value]="theme.id" name="optTema">. I get the 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"> How much ng-reflect-form-control-name it is not I who place.... it is placed at execution time

  • Have you tried to take the formNameControland leave the name? 'Cause I do a "Gambia" to pick up the value of the input.

  • I saw that you are using "[]" brackets in the words inside the input, could you explain what each of them does in their input? Maybe we can change that part and make it work

  • Sure! I’m following this https://angular.io/guide/template-syntax . I may be missing something. In my initial post you may notice that each of the [ ] is a @Input() ....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.