我尝试在单选按钮上创建属性绑定,但得到此错误:ExpressionChangedAfterItHasBeenCheckedError

时间:2018-02-16 17:33:06

标签: angular

我需要根据我拥有的变量选择单选按钮,使用控件的值。

我尝试执行此属性绑定但得到错误:

 private turnoId = 0;
      private nivelId = 2;

和html:

<div class="form-group row">
<label class="col-sm-2 form-control-label">Turnos</label>
<div class="col-sm-10">
 <label  class="radio-inline col-sm-3" *ngFor="let turno of turnos">
<input  #vl [checked]="vl.value == turnoId" type="radio" name="radioTurno" [value]="turno.id" (change)="onSelectionChangeTurno(turno)"> {{ turno.descricao }}
                                        </label>
                                    </div>
                                </div>
                                <div class="line"></div>
                                <div class="form-group row">
                                    <label class="col-sm-2 form-control-label">Nível de Acesso</label>
                                    <div class="col-sm-10">
                                        <label class="radio-inline col-sm-3" *ngFor="let nivelAcesso of nivelAcessoList" >
                                            <input  #vl2 [checked]="vl2.value == nivelId" type="radio" name="radioNivelAcesso" [value]="nivelAcesso.id" (change)="onSelectionChangeNivelAcesso(nivelAcesso)"> {{ nivelAcesso.ds_nivel_acesso }}
                                        </label>
                                    </div>
                                </div>

2 个答案:

答案 0 :(得分:0)

正确绑定:

[checked]="nivelAcesso.id == nivelId"

答案 1 :(得分:0)

 <label class="radio-inline col-sm-3" *ngFor="let nivelAcesso of nivelAcessoList" >              <input  #vl2 
      [checked]="checkId(v12.value,turnoId)" type="radio" name="radioNivelAcesso" 
  [value]="nivelAcesso.id" (change)="onSelectionChangeNivelAcesso(nivelAcesso)"> {{ nivelAcesso.ds_nivel_acesso }}  </label>

在组件中添加此方法:

checkId(a?:number,b?:number){
      if(a==b){
   return true;
      }
       return false;

     }