如何在Angular11material中的Stepper中获取formControlName值形式?
How to get formControlName value forms in Stepper in Angular 11 material?
我使用 angular material 11,我使用步进器 material 组件 https://material.angular.io/components/stepper/examples。
我想在第一步中获取选定的值,并且我想在第一步中向用户显示他选择的值的消息。但我不能这样做。
这是我的 component.html 代码:
<mat-horizontal-stepper [linear]="isLinear" #stepper="matHorizontalStepper">
<mat-step [stepControl]="firstFormGroup">
<form [formGroup]="firstFormGroup">
<ng-template matStepLabel>Fill out your name</ng-template>
<mat-form-field>
<input matInput placeholder="Last name, First name" formControlName="firstCtrl" required>
</mat-form-field>
<mat-select formControlName="sport" placeholder="your favorite sport" (change)="sportHandler($event)" required>
<ng-container >
<mat-option value="F">football</mat-option>
<mat-option value="B">basketball</mat-option>
</ng-container>
</mat-select>
<div>
<button mat-button matStepperNext>Next</button>
</div>
</form>
</mat-step>
<mat-step [stepControl]="secondFormGroup">
<form [formGroup]="secondFormGroup">
<ng-template matStepLabel>Fill out your address</ng-template>
<p>{{pselectedSport != '' ? 'The amount for practicing ' : ''}} {{pselectedSport != '' ? pselectedSport + ' is ': ''}}{{pselectedPrix != '' ? pselectedPrix : ''}}{{pselectedPrix != '' ? '$' : ''}}</p>
<mat-form-field>
<input matInput placeholder="Address" formControlName="secondCtrl" required>
</mat-form-field>
<div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</div>
</form>
</mat-step>
<mat-step>
<ng-template matStepLabel>Done</ng-template>
You are now done.
<div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button (click)="stepper.reset()">Reset</button>
</div>
</mat-step>
</mat-horizontal-stepper>
还有我的 component.ts 代码:
firstFormGroup: FormGroup;
secondFormGroup: FormGroup;
pselectedSport:string = '';
pselectedPrice:string = '';
constructor(private _formBuilder: FormBuilder) { }
ngOnInit() {
this.firstFormGroup = this._formBuilder.group({
firstCtrl: ['', Validators.required],
sport: ['', Validators.required],
});
this.secondFormGroup = this._formBuilder.group({
secondCtrl: ['', Validators.required]
});
}
form1(){
console.log(this.firstFormGroup.value);
}
form2(){
console.log(this.secondFormGroup.value);
}
sportHandler(event: any) {
//update the ui
this.pselectedSport = event.target.value;
if(this.pselectedSport == 'football') {
this.pselectedPrice = '15';
} else {
this.pselectedPrice = '50';
}
console.log('sport : '+this.pselectedSport);
console.log('price : '+this.pselectedSport);
}
问题是我如何在 mat-selected 中获取选定的值,并将该值传递给 typescript 文件中的 pselectedSport,然后在第二步中显示它。
MatSelect 的输出事件是 selectionChange
,而不是 change
。所以你需要改变你的代码:
<mat-select formControlName="sport" placeholder="your favorite sport" (selectionChange)="sportHandler($event)" required>
<ng-container >
<mat-option value="F">football</mat-option>
<mat-option value="B">basketball</mat-option>
</ng-container>
</mat-select>
传递给您的处理函数的事件将是 MatSelectChange
类型,因此要获取值只需访问 value
属性.
我使用 angular material 11,我使用步进器 material 组件 https://material.angular.io/components/stepper/examples。 我想在第一步中获取选定的值,并且我想在第一步中向用户显示他选择的值的消息。但我不能这样做。 这是我的 component.html 代码:
<mat-horizontal-stepper [linear]="isLinear" #stepper="matHorizontalStepper">
<mat-step [stepControl]="firstFormGroup">
<form [formGroup]="firstFormGroup">
<ng-template matStepLabel>Fill out your name</ng-template>
<mat-form-field>
<input matInput placeholder="Last name, First name" formControlName="firstCtrl" required>
</mat-form-field>
<mat-select formControlName="sport" placeholder="your favorite sport" (change)="sportHandler($event)" required>
<ng-container >
<mat-option value="F">football</mat-option>
<mat-option value="B">basketball</mat-option>
</ng-container>
</mat-select>
<div>
<button mat-button matStepperNext>Next</button>
</div>
</form>
</mat-step>
<mat-step [stepControl]="secondFormGroup">
<form [formGroup]="secondFormGroup">
<ng-template matStepLabel>Fill out your address</ng-template>
<p>{{pselectedSport != '' ? 'The amount for practicing ' : ''}} {{pselectedSport != '' ? pselectedSport + ' is ': ''}}{{pselectedPrix != '' ? pselectedPrix : ''}}{{pselectedPrix != '' ? '$' : ''}}</p>
<mat-form-field>
<input matInput placeholder="Address" formControlName="secondCtrl" required>
</mat-form-field>
<div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</div>
</form>
</mat-step>
<mat-step>
<ng-template matStepLabel>Done</ng-template>
You are now done.
<div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button (click)="stepper.reset()">Reset</button>
</div>
</mat-step>
</mat-horizontal-stepper>
还有我的 component.ts 代码:
firstFormGroup: FormGroup;
secondFormGroup: FormGroup;
pselectedSport:string = '';
pselectedPrice:string = '';
constructor(private _formBuilder: FormBuilder) { }
ngOnInit() {
this.firstFormGroup = this._formBuilder.group({
firstCtrl: ['', Validators.required],
sport: ['', Validators.required],
});
this.secondFormGroup = this._formBuilder.group({
secondCtrl: ['', Validators.required]
});
}
form1(){
console.log(this.firstFormGroup.value);
}
form2(){
console.log(this.secondFormGroup.value);
}
sportHandler(event: any) {
//update the ui
this.pselectedSport = event.target.value;
if(this.pselectedSport == 'football') {
this.pselectedPrice = '15';
} else {
this.pselectedPrice = '50';
}
console.log('sport : '+this.pselectedSport);
console.log('price : '+this.pselectedSport);
}
问题是我如何在 mat-selected 中获取选定的值,并将该值传递给 typescript 文件中的 pselectedSport,然后在第二步中显示它。
MatSelect 的输出事件是 selectionChange
,而不是 change
。所以你需要改变你的代码:
<mat-select formControlName="sport" placeholder="your favorite sport" (selectionChange)="sportHandler($event)" required>
<ng-container >
<mat-option value="F">football</mat-option>
<mat-option value="B">basketball</mat-option>
</ng-container>
</mat-select>
传递给您的处理函数的事件将是 MatSelectChange
类型,因此要获取值只需访问 value
属性.