FormControlName not working with ion-radio

2019-08-14 14:31发布

New:

So I can get it to work withou a radio-group or withouth the right bindings. I can't get it to work with both.

This is allowed:

<div *ngFor="let column of row.controls.columns.controls; let j = index">
                <ion-list radio-group [formControlName]="'col'+j">
                    <ion-radio></ion-radio>
                </ion-list>
            </div>

But then I don't have a list with a radio-group, it doesn't matter what I change it will always break. If I move the list outside it breaks, move the formControlName it breaks. I really don't know how to get this to work.

I would think that this should be the way to go:

<ion-list radio-group>
            <div *ngFor="let column of columns; let j = index">
                <div [formControlName]="'col'+j">
                    <ion-radio></ion-radio>
                </div>
            </div>
        </ion-list>

But again the same error:

Error: No value accessor for form control with path: 'rows -> 0 -> col0'

i = the amount of rows I need. j = the amount of options/questions I need.

Old:

I am trying to use the ion-radio as a input field in a formgroup but it keeps giving me the error:

No value accessor for form control with path ...

So I switched to an input with type="radio", but if I want to group those, I need to give them the same name, which I can't because I am using a *ngFor and otherwise my formControlName is not correct anymore.

Can anybody help me to get the ion-radio to work? It would look better and easier to group.

<ion-col radio-group class="radioGroup" col-6>
   <ion-col *ngFor="let column of columns; let j = index">
        <input value="X" formControlName="col{{j + 1}}" type="radio" />
        <!-- <ion-radio value="X"></ion-radio> -->
    </ion-col>
</ion-col>

Creation:

this.inspectionTableForm = this.formBuilder.group({
    header: this.formBuilder.group({
        title: [this.question.properties.header[0]],
        columns: this.formBuilder.array([
                    this.formBuilder.control('')
                ]),
        comments: ['']
    }),
    rows: this.formBuilder.array([
        this.initRow()
    ])
});

private initRow() {
    // Create the temp control
    let tempControl = this.formBuilder.group({
        title: '',
    });

    // Create an array for the columns
    let arr = [];
    for (let index = 0; index < this.question.properties["number-of-columns"]; index++) {
        // Push a control to the array and also one for the value on a higher level
        arr.push(this.columns[index])
        tempControl.addControl(`col${index}`, this.formBuilder.control('')) // Col-1-2-3
    };
    tempControl.addControl('columns', this.formBuilder.array(arr)) //Array

    // Check if we need to add the comment field
    if (this.question.properties["comment-field"]) {
        tempControl.addControl('comment', this.formBuilder.control(''))
    }
    return tempControl;
}

Edit semi working new code (thanks xrobert35):

<ion-col radio-group [formControlName]="'col'+i">
    <ion-item *ngFor="let column of row.controls.columns.controls">
        <ion-radio></ion-radio>
    </ion-item>
</ion-col>

This is what I get when I press the first option, then second and then third:

enter image description here

It updates the same col with some weird value. I think the problem is that I do not need i as index but j, but it breaks when I do that. Something like this:

<ion-col radio-group >
    <ion-item *ngFor="let column of row.controls.columns.controls; let j = index" [formControlName]="'col'+j">
        <ion-radio></ion-radio>
    </ion-item>
</ion-col>

No value accessor for form control with path: 'rows -> 0 -> col0'

2条回答
劳资没心,怎么记你
2楼-- · 2019-08-14 14:56

You can't use the directive formControlName with ion-radio cause you can only use it on a component which work with ngModel.

I understand that you want a list of ion-radio but only one value bind into the formGroup. So you can use a ion-list component like in the documentation ? and replace the [(ngModel)] by formGroupName

<ion-list radio-group formControlName="myFormGroupName">
  <ion-item>
    <ion-label>Friends</ion-label>
    <ion-radio value="friends" checked></ion-radio>
  </ion-item>
  <ion-item>
    <ion-label>Family</ion-label>
    <ion-radio value="family"></ion-radio>
  </ion-item>
  <ion-item>
    <ion-label>Enemies</ion-label>
    <ion-radio value="enemies" [disabled]="isDisabled"></ion-radio>
  </ion-item>
</ion-list>

https://ionicframework.com/docs/api/components/radio/RadioButton/

New code Your html should look like something like this

<div [fromGroup]="inspectionTableForm">

<div *ngFor"let question of questions; let indexRow=index" class="row" formArrayName="rows">
   <span> {{question.libelle}} </span>
   <ion-list radio-group [formControlName]="'answer'+indexRow">
       <ion-item *ngFor="let answer of question.answers">
           <ion-label>{{answer.labelle}}</ion-label>
           <ion-radio [value]="answer.value"></ion-radio>
       </ion-item>
   </ion-list>
   <textarea formControlName="comment">
</div>

</div>
查看更多
对你真心纯属浪费
3楼-- · 2019-08-14 15:00

if you have array of input and you use reactive methode it's better to use FormArray inside ur FormGroup instead of use this way . this documentation will be help you to implement FormGroup in ur code : https://angular.io/api/forms/FormArray

查看更多
登录 后发表回答