在具有angular2的mat-radio-group中设置mat-radio-button默认选项

时间:2018-02-27 16:47:23

标签: angular angular-material

我在角度应用中遇到了这个问题。我在mat-radio-group中有很多选项,但这些选项是根据json对象动态放置的。像这样:

这是json对象

{
  "list": [
    {"name": "some name 1", ID: "D1"},
    {"name": "some name 2", ID: "D2"}
  ]
}

在这个例子中,我的列表中只有两个对象,但可能是9个,20个或任意数量的对象。

所以我希望在我的html中,无论有多少个对象,只选择第一个,即使列表只改变第一个对象保持选中状态。

这是我的HTML:

<mat-radio-group name="opList"  fxLayout="column">
  <mat-radio-button *ngFor="let op of listOfOptions"  name="opList" >{{ op.name}}</mat-radio-button>
</mat-radio-group>

listOfOptions变量具有JSON对象。

如何始终设置所选的第一个对象?

7 个答案:

答案 0 :(得分:7)

在JSON中添加{ "list": [ {"name": "some name 1", ID: "D1", "checked": true}, {"name": "some name 2", ID: "D2", "checked": false} ] } 属性

    <mat-radio-group name="opList"  fxLayout="column">
      <mat-radio-button *ngFor="let op of listOfOptions" 
      [checked]="op.checked" name="opList" >{{ op.name}}</mat-radio-button>
    </mat-radio-group>

然后

App

答案 1 :(得分:5)

使用[value]="op.name"并对您的Component变量进行绑定,例如[(ngModel)]="chosenItem"

HTML:

<mat-radio-group name="opList"  fxLayout="column" [(ngModel)]="chosenItem">
      <mat-radio-button *ngFor="let op of list" [value]="op.name" name="opList" >{{ op.name}}</mat-radio-button>
  </mat-radio-group>

在你的组件中:

list = [
    { "name": "some name 1", ID: "D1"},
    { "name": "some name 2", ID: "D2"}
]
chosenItem = this.list[0].name;

答案 2 :(得分:2)

对于那些仍在寻找正确答案的人,我将遵循以下文档:here 在ngOnInit()上加了一点:

它去了-TS:

import {Component, OnInit} from '@angular/core';


@Component({
  selector: 'radio-btns-test',
  templateUrl: 'radio-btns-test.html',
  styleUrls: ['radio-btns-test.css'],
})
export class RadioBtnsTest implements OnInit {
                             // data source for the radio buttons:
  seasons: string[] = ['Winter', 'Spring', 'Summer', 'Autumn'];

                             // selected item
  favoriteSeason: string;

                             // to dynamically (by code) select item
                             // from the calling component add:
  @Input() selectSeason: string;



  ngOnInit() {
                             // To have a default radio button checked
                             // at the page loading time (fixed solution)
      this.favoriteSeason = 'Summer';

                             // OR  (do only one)
                             // To dynamically (by code) select item
                             // from the calling component add:
      this.favoriteSeason = this.selectSeason;
  }

}

HTML:

<label id="example-radio-group-label">Pick your favorite season</label>

<mat-radio-group
  aria-labelledby="example-radio-group-label"
  class="example-radio-group"
  [(ngModel)]="favoriteSeason">

  <mat-radio-button 
       class="example-radio-button" 
       *ngFor="let season of seasons" 
       [value]="season">
    {{season}}
  </mat-radio-button>

</mat-radio-group>

<div>Your favorite season is: {{favoriteSeason}}</div>

CSS

.example-radio-group {
  display: flex;
  flex-direction: column;
  margin: 15px 0;
}

.example-radio-button {
  margin: 5px;
}

为完成该示例-从另一个组件启动radio-btn-test,如下所示:

<radio-btns-test [selectSeason]="'Summer'"></radio-btns-test>

答案 3 :(得分:0)

let i = index添加到*ngFor,然后将[value][checked]钩到它。

<mat-radio-group>
  <mat-radio-button *ngFor="let o of options; let i = index" [value]="i" [checked]="i === 0">
<mat-radio-group>

这将选中第一个复选框。

答案 4 :(得分:0)

我也有这个问题。我使用的是角形材质7,并尝试使用[checked] =“ i == 0”,但由于某些原因,它不起作用。我还尝试将新的布尔字段添加到名为isDefault的JSON对象中,然后使用[checked] =“ obj.isDefault”,但没有结果。 最后,我终于通过设置OnInit方法的默认值解决了它。 我知道这意味着更多的代码,但这是为我解决的原因。

here you can see an example

答案 5 :(得分:0)

使用 checkedtrue 设置材料单选按钮值默认选择

示例:

<mat-radio-group>
     <mat-radio-button [checked]="true"> Yes </mat-radio-button>
     <mat-radio-button> No </mat-radio-button>
</mat-radio-group>

答案 6 :(得分:-1)

您可以在HTML中的第一个单选按钮中使用[checked] ='true'。

如果要更改检查值,则还可以动态设置它。

 <mat-radio-group >
     <mat-radio-button [checked]='true'>yes </mat-radio-button>
</mat-radio-group>
相关问题