How can we set the default value of radio button in angular 2

这一生的挚爱 提交于 2019-12-24 00:54:20

问题


I have a Model driven form and want to show the fields based on radio input. Code :-

       <div class="form-group">
          <md-radio-group class="form-control"  id="radio1" formControlName="selection">
            <md-radio-button  [value]="A" >A</md-radio-button>
            <md-radio-button  [value]="B">B</md-radio-button>
          </md-radio-group>
        </div>

        <div class="form-group" *ngIf="selection.value=='A'">
          <md-input-container>
              <textarea md-input placeholder="A" class="form-control"
                        formControlName="A"
                        style="border:none;"></textarea>
          </md-input-container>
        </div>
        <div class="form-group" *ngIf="selection.value=='B'">
          <md-input-container>
            <input md-input placeholder="B" class="form-control"
                   formControlName="B"
                   style="border:none;">
          </md-input-container>            
        </div>

Getting error as "Cannot read property 'value' of undefined" As no radio is selected.

How can I specify to select the first one as default.


回答1:


You need to pass a string as a value for the radio. Right now Angular tries to find a variable with the name A.

<md-radio-group class="form-control" [(ngModel)]="selection.value" 
                id="radio1" formControlName="selection">
  <md-radio-button [value]="'A'">A</md-radio-button>
  <md-radio-button [value]="'B'">B</md-radio-button>
</md-radio-group>

You initialize the radio as you assign a value to selection.value in the according component:

class MyRadioComponent {
  selection = {"value":"A"};
}



回答2:


Try this:

this.complexForm = fb.group({
                       'name': [null, Validators.required],
                       'description': [null, Validators.required],
                       'file': ["A", Validators.required],
                   });



回答3:


You are getting this error because selection formControl is not yet created.There are two possible solution for this.

  1. Use safe navigation operator ( ?. ) as *ngIf=selection?.value=='A' while accessing the value of radio.

    <div class="form-group" *ngIf="selection?.value=='A'">
      <md-input-container>
          <textarea md-input placeholder="A" class="form-control"
                    formControlName="A"
                    style="border:none;"></textarea>
      </md-input-container>
    </div>
    
  2. Prepare your form before the view is rendered.(i.e prepare your form in constructor of the component.)

and default value to any form control can be given when we create the form control.

for example in your case you can set default value of selection form control as follows.

let myForm = new FormGroup({ selection : new FormControl('A') }) 


来源:https://stackoverflow.com/questions/41558196/how-can-we-set-the-default-value-of-radio-button-in-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!