要调整两个 mat-select 输入之间的引用,您可以使用 Angular Material 提供的 mat-select 组件以及 Angular 的表单控制器。
首先,确保您已经安装了 Angular Material,并将其导入到您的模块中。
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatSelectModule } from '@angular/material/select';
@NgModule({
imports: [
FormsModule,
ReactiveFormsModule,
MatSelectModule
]
})
export class AppModule { }
接下来,在您的组件中,使用 FormControl 控制器创建两个输入。
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent implements OnInit {
firstSelect: FormControl;
secondSelect: FormControl;
ngOnInit() {
this.firstSelect = new FormControl();
this.secondSelect = new FormControl();
}
}
然后,在您的 HTML 模板中,将这两个输入与 mat-select 组件绑定起来。
First Select
Option 1
Option 2
Option 3
Second Select
Option 4
Option 5
Option 6
现在,您可以使用这两个输入来进行任何操作,例如获取选定的值或将其与其他表单元素进行比较。
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent implements OnInit {
firstSelect: FormControl;
secondSelect: FormControl;
ngOnInit() {
this.firstSelect = new FormControl();
this.secondSelect = new FormControl();
this.firstSelect.valueChanges.subscribe(value => {
console.log('First Select:', value);
});
this.secondSelect.valueChanges.subscribe(value => {
console.log('Second Select:', value);
});
}
}
通过这种方式,您可以根据需要使用这两个输入的值。