Angular 2 md-radio-button设置值以编程方式

时间:2017-06-20 09:16:46

标签: angular angular-material2

我正在尝试以md-radio-button内的md-radio-group编程设置值。这是我简化的代码:

<md-radio-group [(ngModel)]="radioGroupValue">
    <div *ngFor="let answer of answers; let i = index">
       <md-checkbox *ngIf="isMultiple" [(ngModel)]="answer.isCorrect" [value]="i" ngDefaultControl></md-checkbox>
       <md-radio-button *ngIf="!isMultiple" [(ngModel)]="answer.isCorrect" [value]="i" ngDefaultControl></md-radio-button>
       {{ answer.text }}
    </div>
</md-radio-group>

当我将isMultipletrue更改为false时,我也会更新radioGroupValue。第一次,没有选中md-radio-button,如果我再次将isMultiple更改为truefalse,则会检查右侧单选按钮。我该怎么处理?

1 个答案:

答案 0 :(得分:2)

要检查md-radio-group我们需要在html模板上使用[checked]="boolean"

<md-input-container [checked]="answer.isCorrect"> {{ answer.text }}</md-radio-button>