Ser*_*dez 5 angular-material angular
我在我的角度应用程序中遇到了这个问题。我在mat-radio-group中有很多选项,但是这些都是根据json对象动态放置的。像这样:
这是json对象
{
"list": [
{"name": "some name 1", ID: "D1"},
{"name": "some name 2", ID: "D2"}
]
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,列表中只有两个对象,但可以是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>
Run Code Online (Sandbox Code Playgroud)
该listOfOptions变量具有JSON对象。
我如何始终将选择的第一个对象设置为?
Csa*_*aba 13
添加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>
Run Code Online (Sandbox Code Playgroud)
这将检查第一个单选按钮。
fil*_*rak 11
checked在JSON中添加属性
{
"list": [
{"name": "some name 1", ID: "D1", "checked": true},
{"name": "some name 2", ID: "D2", "checked": false}
]
}
Run Code Online (Sandbox Code Playgroud)
然后
<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>
Run Code Online (Sandbox Code Playgroud)
使用checked属性 astrue将单选按钮设置为默认选择。
例子:
<mat-radio-group>
<mat-radio-button [checked]="true"> Yes </mat-radio-button>
<mat-radio-button> No </mat-radio-button>
</mat-radio-group>
Run Code Online (Sandbox Code Playgroud)
小智 7
使用 [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>
Run Code Online (Sandbox Code Playgroud)
在您的组件中:
list = [
{ "name": "some name 1", ID: "D1"},
{ "name": "some name 2", ID: "D2"}
]
chosenItem = this.list[0].name;
Run Code Online (Sandbox Code Playgroud)
您可以将 [checked]='true' 与 HTML 中的第一个单选按钮一起使用。
如果你想改变检查值,那么你也可以动态设置它。
<mat-radio-group >
<mat-radio-button [checked]='true'>yes </mat-radio-button>
</mat-radio-group>
Run Code Online (Sandbox Code Playgroud)
对于那些仍在寻找正确答案的人,我将遵循文档:此处 在 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;
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
CSS
.example-radio-group {
display: flex;
flex-direction: column;
margin: 15px 0;
}
.example-radio-button {
margin: 5px;
}
Run Code Online (Sandbox Code Playgroud)
为了完成该示例 - 从另一个组件启动 radio-btn-test ,请像这样调用它:
<radio-btns-test [selectSeason]="'Summer'"></radio-btns-test>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32009 次 |
| 最近记录: |