我想了解如何访问模板中所选项目的值。具体来说,我想知道如何在模板中访问IPMIDisplayTime
和IPMIDisplayTime
的选定值,并在此过程中使用它。
import {ViewChild, ElementRef} from '@angular/core';
@Component({
selector: 'app-select-dialog',
template:`<h1 mat-dialog-title>
{{ title | translate }}
</h1>
<div mat-dialog-content>
<mat-select #IPMIDisplayTime name="name" placeholder="optionPlaceHolder" [(ngModel)]="IPMIDisplayTimeSelection">
<mat-option *ngFor="let option of options" [value]="options.value">
{{ option.label }}
</mat-option>
</mat-select>
</div>
<div mat-dialog-actions>
<span fxFlex></span>
<button class="mat-raised-button mat-accent" (click)="dialogRef.close(false)">{{"Close" | translate}}</button>
<span fxFlex></span>
<button class="mat-raised-button mat-accent" (click)="dialogRef.close(true)">{{"OK" | translate}}</button>
</div>`,
styleUrls : [ './select-dialog.component.scss' ]
})
export class SelectDialogComponent {
public title: string;
public options: Array<{ label: string, value: string }>;
public optionPlaceHolder: string;
public method: string;
public params: string;
@ViewChild('IPMIDisplayTime') IPMIDisplayTimeSelect: ElementRef;
IPMIDisplayTimeSelection: string;
constructor(public dialogRef: MatDialogRef < SelectDialogComponent >, protected translate: TranslateService ) {
}
}
答案 0 :(得分:2)
您在[(ngModel)]
组件上使用<mat-select>
双向绑定。这意味着IPMIDisplayTimeSelection
将始终与当前<mat-select>
值匹配。
您不需要@ViewChild
装饰器和#IPMIDisplayTime
模板变量。
someFn() {
// this is the value of your select component
console.log(this.IPMIDisplayTimeSelection);
}