gpt4 book ai didi

angular - 如何在 Angular 6 中默认选择 mat-button-toggle

转载 作者:行者123 更新时间:2023-12-04 01:46:16 24 4
gpt4 key购买 nike

我有一个响应式(Reactive) angular-6 形式,我在其中使用了 mat-button-toggle-group

<mat-button-toggle-group #group="matButtonToggleGroup"  [(value)]="myFlagForButtonToggle" (change)="onEndpointValChange(group.value)"  multiple=false formControlName='endpoints' [(ngModel)]="myFlagForButtonToggle">
<mat-button-toggle *ngFor="let item of endpointToggleOptions;" [value]="item">{{item}}</mat-button-toggle>
</mat-button-toggle-group>

在我的ts文件中

  myFlagForButtonToggle: String[] = ["Single"];
endpointToggleOptions: Array<String> = ["Single", "Multiple"];

但在 UI 中,切换按钮的编号默认处于选中状态。

求助,我做错了什么

我的模型是这样的

this.outerForm = this._formBuilder.group({
firstFormGroup: this._formBuilder.group({
pidNumber: ['', [Validators.pattern(this.spacePattern)]],
}),
secondFormGroup: this._formBuilder.group({
endpoints: ['', [Validators.required]]
})
});

编辑:我希望 mat-button-toggle-group 也与我的模型绑定(bind)。

最佳答案

Here是一个工作示例:

HTML代码:

<form [formGroup]="outerForm">
<div formGroupName="secondFormGroup">
<mat-button-toggle-group #group="matButtonToggleGroup" (change)="onEndpointValChange(group.value)" multiple="false" formControlName='endpoints'
[(ngModel)]="myFlagForButtonToggle">
<mat-button-toggle *ngFor="let item of endpointToggleOptions;" [value]="item">{{item}}</mat-button-toggle>
</mat-button-toggle-group>
</div>
</form>

TS代码:

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

import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';

/**
* @title Exclusive selection
*/
@Component({
selector: 'button-toggle-exclusive-example',
templateUrl: 'button-toggle-exclusive-example.html',
styleUrls: ['button-toggle-exclusive-example.css'],
})
export class ButtonToggleExclusiveExample {

myFlagForButtonToggle: String = "Single";
endpointToggleOptions: Array<String> = ["Single", "Multiple"];
outerForm: FormGroup;
constructor(private _formBuilder: FormBuilder) {
this.outerForm = this._formBuilder.group({
firstFormGroup: this._formBuilder.group({ pidNumber: new FormControl(''), }), secondFormGroup:
this._formBuilder.group({ endpoints: new FormControl(''), })
});
}
}

关于angular - 如何在 Angular 6 中默认选择 mat-button-toggle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55155096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com