Hel*_*ene 2 datepicker typescript angular
我有这样的选择:
<mat-select placeholder="..." (change)="onChange($event.value)">
<mat-option *ngFor="let option of options" [value]="option.value">
{{ option }}
</mat-option>
</mat-select>
<div class="disable">
<mat-form-field>
<input matInput [matDatepicker]="picker" placeholder="Choose a date">
<mat-datepicker #picker ></mat-datepicker>
</mat-form-field>
</div>
Run Code Online (Sandbox Code Playgroud)
禁用类只是隐藏div的包含。
在我的onChange($ event)中:
onChange(value) {
if(value === 'someValue'){
openDatePicker() //How to do this ??
}
}
Run Code Online (Sandbox Code Playgroud)
如果选择了特定值,我想打开日期选择器。可以通过打字稿做到这一点吗?
感谢您的答复
您将需要使用ViewChild以下方式在代码中获取日期选择器:
@ViewChild('picker') datePicker: MatDatepicker<Date>;
Run Code Online (Sandbox Code Playgroud)
然后在函数中,可以调用open()datepicker 的方法:
onChange(value) {
if(value === 'someValue'){
this.datePicker.open();
}
}
Run Code Online (Sandbox Code Playgroud)
链接到StackBlitz演示。
ViewChild在TS中使用以下命令创建日期选择器对象:
@ViewChild('picker') datePicker: MatDatepicker;
Run Code Online (Sandbox Code Playgroud)
使用预定义方法open为MatDatepicker:
onChange(value) {
if(value === 'someValue'){
this.datePicker.open();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2646 次 |
| 最近记录: |