简体   繁体   English

如何在组件的角度2材质上设置数据选择器输入?

[英]how can I set a datapicker input on angular 2 material from a component?

I've already got the content that is sent by a form 我已经有通过表单发送的内容

home.component.ts home.component.ts

...

constructor(private route: ActivatedRoute){}

/**
* Get the names OnInit
*/
ngOnInit() {


  this.form= {
    postcode: this.route.snapshot.params['postcode'],
    date_from: this.route.snapshot.params['date_from'],
    date_to: this.route.snapshot.params['date_to']
  }

  console.log( this.form); // {postcode: "WEDSW", date_from: "11/09/2017", date_to: "16/09/2017"}
}

now what I need to do it's to populate it doing something like <input value="{{form.data_from}}"> but it wont work, when I open the datepicker it will show the current day and not the value that has been set in the form object 现在,我需要做的是使用<input value="{{form.data_from}}">类的方法填充它,但是它将无法正常工作,当我打开日期选择器时,它将显示当前日期,而不是已经显示的值。在form对象中设置

I'm also getting a value not recognized as a date object by DateAdapter that I think could be solve doing this 我也得到了value not recognized as a date object by DateAdapter ,我认为可以解决此问题

home.component.html home.component.html

 <div>
     <div class="calendar"> 
        <button md-raised-button (click)="pickupDate.open()" class="calendar__ico"></button>
        <button md-raised-button (click)="pickupDate.open()"></button>
    </div>

    <md-form-field>
     <input mdInput [mdDatepicker]="pickupDate">
     <md-datepicker #pickupDate></md-datepicker>
    </md-form-field>
   </div>

   <div>
    <div class="calendar">  
     <button md-raised-button (click)="returnDate.open()"></button>
     <button md-raised-button (click)="returnDate.open()"></button>
   </div>

   <md-form-field>
     <input mdInput [mdDatepicker]="returnDate">
     <md-datepicker #returnDate></md-datepicker>
   </md-form-field>
  </div>

You might get some idea from this snippet from angular material docs https://github.com/angular/material2 . 您可能会从角度材料文档https://github.com/angular/material2的此片段中获得一些想法。

.html .html

<h2>Result</h2>
<p>
  <md-datepicker-toggle [for]="resultPicker"></md-datepicker-toggle>
  <md-form-field>
    <input mdInput #resultPickerModel="ngModel" [mdDatepicker]="resultPicker" [(ngModel)]="date" placeholder="Pick a date" (dateInput)="onDateInput($event)" (dateChange)="onDateChange($event)">
    <md-datepicker #resultPicker [startAt]="startAt">
    </md-datepicker>
    <md-error *ngIf="resultPickerModel.hasError('mdDatepickerParse')">
      "{{resultPickerModel.getError('mdDatepickerParse').text}}" is not a valid date!
    </md-error>
    <md-error *ngIf="resultPickerModel.hasError('mdDatepickerMin')">Too early!</md-error>
    <md-error *ngIf="resultPickerModel.hasError('mdDatepickerMax')">Too late!</md-error>
    <md-error *ngIf="resultPickerModel.hasError('mdDatepickerFilter')">Date unavailable!</md-error>
  </md-form-field>
</p>
<p>Last input: {{lastDateInput}}</p>
<p>Last change: {{lastDateChange}}</p>

.ts .ts

import { MdDatepickerInputEvent } from '@angular/material';

...

  startAt: Date;
  date: Date;
  lastDateInput: Date | null;
  lastDateChange: Date | null;

  onDateInput = (e: MdDatepickerInputEvent<Date>) => this.lastDateInput = e.value;
  onDateChange = (e: MdDatepickerInputEvent<Date>) => this.lastDateChange = e.value;

browser 浏览器

在此处输入图片说明

You can bind to [(ngModel)] and set it to the values you get on your form: 您可以绑定到[(ngModel)]并将其设置为在表单上获得的值:

template 模板

<md-form-field>
    <input mdInput [mdDatepicker]="picker" placeholder="Choose a date" [(ngModel)]="initialDate">
    <md-datepicker-toggle mdSuffix [for]="picker"></md-datepicker-toggle>
    <md-datepicker #picker></md-datepicker>
</md-form-field>

component 零件

initialDate = new Date(2017,10,30);
//change with your date_from, date_to

Working plunker here 在这里工作的家伙

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Angular的组件文件中设置输入占位符文本 - How can I set input placeholder text in component file in Angular 如何防止 Angular 8+ 组件在设置 Input 属性并且 ViewChild 准备好之前执行 - How can I prevent an Angular 8+ component from executing until an Input property is set and a ViewChild is ready 我无法从文件输入 @angular/material 组件中获取文件名 - I can't get the file name from the File Input @angular/material component 如何将Angular FormBuilder与Angular Material mat-datapicker一起使用 - How to use Angular FormBuilder with Angular Material mat-datapicker 如何在角度2材料设计中设置md-select组件的默认值? - How do I set default value on md-select component from angular 2 material design? 如何在具有不同配置的两页中使用angular 2材质设计Datapicker? - How to use angular 2 material design Datapicker in two pages with different configuration? Angular 8:如何设置 angular 材料日期选择器的时间? - Angular 8: How can I set time for angular material datepicker? 如何在 angular 中测试组件 @Input? - How can I test component @Input in angular? 如何将Angular Material自动完成中的值分配给组件中的变量? - How can I assign a value from the Angular Material autocomplete to a variable in my component? 如何将自定义属性关联到Angular Material组件? - How can I associate custom properties to an Angular Material component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM