Html 如何在typescript文件中的一组单选按钮中选择特定的单选按钮?
每当从页面上的下拉列表中选择某个内容时,我想选择一个特定的单选按钮。如何通过在下拉选择中调用的typescript中的函数来执行此操作?如何执行此操作将取决于单选按钮的设置方式,但一种方法是让您的Html 如何在typescript文件中的一组单选按钮中选择特定的单选按钮?,html,angular,typescript,Html,Angular,Typescript,每当从页面上的下拉列表中选择某个内容时,我想选择一个特定的单选按钮。如何通过在下拉选择中调用的typescript中的函数来执行此操作?如何执行此操作将取决于单选按钮的设置方式,但一种方法是让您的mat单选组通过[(ngModel)]跟踪变量,您可以在下拉调用的任何函数中更改该函数 请参阅一个简单的示例,说明如何布置这类事情 至于下拉列表,您需要查看(change)或(ngModelChange),要执行示例按钮对(单击)所做的操作,就像HammerN'Songs建议的那样,角度方法是将值绑定到
mat单选组通过[(ngModel)]
跟踪变量,您可以在下拉调用的任何函数中更改该函数
请参阅一个简单的示例,说明如何布置这类事情
至于下拉列表,您需要查看(change)
或(ngModelChange)
,要执行示例按钮对(单击)
所做的操作,就像HammerN'Songs建议的那样,角度方法是将值绑定到组件的属性。实际上,您可以将属性绑定到radio和select,如下所示
radio-ng-model.example.ts
radio-ng-model-example.html
radio-ng-model-example.html
h2>使用收音机
选择你最喜欢的季节
{{季节}
你最喜欢的季节是:{{FavoriteSearch}
使用Select
选择你最喜欢的季节
{{季节}
因此,每当有人在下拉列表中选择某个内容时,我只想重置为默认单选按钮(即第一个)。如何使用ng模型表示我希望选择第一个按钮?@cabesuon Oops-现在应该是。
import {Component} from '@angular/core';
@Component({
selector: 'radio-ng-model-example',
templateUrl: 'radio-ng-model-example.html',
styleUrls: ['radio-ng-model-example.css'],
})
export class RadioNgModelExample {
favoriteSeason: string;
seasons: string[] = ['Winter', 'Spring', 'Summer', 'Autumn'];
selectionChange(event) {
console.log(event.value);
}
}
<h2>Using a Radio</h2>
<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>
<h2>Using a Select</h2>
<mat-form-field>
<mat-label>Pick your favorite season</mat-label>
<mat-select [(value)]="favoriteSeason"
(selectionChange)="selectionChange($event)">
<mat-option *ngFor="let season of seasons" [value]="season">{{season}}</mat-option>
</mat-select>
</mat-form-field>
import {Component} from '@angular/core';
@Component({
selector: 'radio-ng-model-example',
templateUrl: 'radio-ng-model-example.html',
styleUrls: ['radio-ng-model-example.css'],
})
export class RadioNgModelExample {
selectSeason: string;
radioSeason: string;
seasons: string[] = ['Winter', 'Spring', 'Summer', 'Autumn'];
selectionChange(event) {
this.radioSeason = this.seasons[0];
}
}
h2>Using a Radio</h2>
<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)]="radioSeason">
<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>
<h2>Using a Select</h2>
<mat-form-field>
<mat-label>Pick your favorite season</mat-label>
<mat-select [(value)]="selectSeason"
(selectionChange)="selectionChange($event)">
<mat-option *ngFor="let season of seasons" [value]="season">{{season}}</mat-option>
</mat-select>
</mat-form-field>