Angular 2 Date Input not binding to date value

后端 未结 10 1927
醉梦人生
醉梦人生 2021-01-30 16:16

trying to get a form set up but for some reason, the Date input in my html is not binding to the object\'s date value, despite using [(ngModel)]

html:

&l         


        
相关标签:
10条回答
  • 2021-01-30 16:28

    Angular 2 completely ignores type=date. If you change type to text you'll see that your input has two-way binding.

    <input type='text' #myDate [(ngModel)]='demoUser.date'/><br>
    

    Here is pretty bad advise with better one to follow:

    My project originally used jQuery. So, I'm using jQuery datepicker for now, hoping that angular team will fix the original issue. Also it's a better replacement because it has cross-browser support. FYI, input=date doesn't work in Firefox.

    Good advise: There are few pretty good Angular2 datepickers:

    • https://github.com/emirdeliz/meus-projetos/tree/master/angular2-schedule/app/frontend/components/datepicker

    • https://github.com/MrPardeep/Angular2-DatePicker

    • https://www.npmjs.com/package/ng2-datepicker

    0 讨论(0)
  • 2021-01-30 16:28

    As per HTML5, you can use input type="datetime-local" instead of input type="date".

    It will allow the [(ngModel)] directive to read and write value from input control.

    Note: If the date string contains 'Z' then to implement above solution, you need to trim the 'Z' character from date.

    For more details, please go through this link on mozilla docs.

    0 讨论(0)
  • 2021-01-30 16:29

    In .ts :

    today: Date;
    
    constructor() {  
    
        this.today =new Date();
    }
    

    .html:

    <input type="date"  
           [ngModel]="today | date:'yyyy-MM-dd'"  
           (ngModelChange)="today = $event"    
           name="dt" 
           class="form-control form-control-rounded" #searchDate 
    >
    
    0 讨论(0)
  • 2021-01-30 16:29

    If you are using a modern browser there's a simple solution.

    First, attach a template variable to the input.

    <input type="date" #date />
    

    Then pass the variable into your receiving method.

    <button (click)="submit(date)"></button>
    

    In your controller just accept the parameter as type HTMLInputElement and use the method valueAsDate on the HTMLInputElement.

    submit(date: HTMLInputElement){
        console.log(date.valueAsDate);
    }
    

    You can then manipulate the date anyway you would a normal date.

    You can also set the value of your <input [value]= "..."> as you would normally.

    Personally, as someone trying to stay true to the unidirectional data flow, i try to stay away from two way data binding in my components.

    0 讨论(0)
  • 2021-01-30 16:31

    In your component

    let today: string;
    
    ngOnInit() {
      this.today = new Date().toISOString().split('T')[0];
    }
    

    and in your html file

    <input name="date" [(ngModel)]="today" type="date" required>
    
    0 讨论(0)
  • 2021-01-30 16:33

    you can use a workaround, like this:

    <input type='date' (keyup)="0" #myDate [(ngModel)]='demoUser.date'/><br>
    

    on your component :

     @Input public date: Date,
    
    0 讨论(0)
提交回复
热议问题