【问题标题】:How to set the date of the date-time picker on page refresh or page change in angular4如何在angular4中的页面刷新或页面更改上设置日期时间选择器的日期
【发布时间】:2018-11-04 15:25:46
【问题描述】:

我正在使用来自 - 的 ng2-date 时间选择器 -

https://www.npmjs.com/package/angular2-datetimepicker

假设我转到位于 page-1 内的 page-2 并且您仅在通过 search-by-date datetime pickers 搜索数据后才获得 page-2 。然后我在第 2 页并取消第 2 页并返回第 1 页,日期时间选择器重置。我试图再次取回相同的日期但无济于事(可能发生我的代码实现不正确)。一旦页面更改,日期时间选择器是否会自行重置。有没有办法在之后取回相同的日期页面刷新或代码中的日期变量也被重置。

所以我的问题是 - 如何将其值设置为与页面更改/刷新之前相同。
我尝试使用 [options] 但无济于事 -

代码-

transaction.component.html

     <div class="form-group " >


                <angular2-date-picker 
    [options]="toDateTransaction"
                 title="Click to enter To Date and Time" 
    [(ngModel)]="date1" [settings]="settings1"
                [(ngModel)]="toDateTransaction"  
    formControlName="toDateTransaction"></angular2-date-picker>
            </div>

transaction.component.ts

export class TransactionComponent implements OnInit{

  date1: Date = new Date();
  settings1 = {
  bigBanner: true,
  timePicker: true,
  format: 'yyyy-MM-dd hh:mm',

   defaultOpen: false
  }


  ngOnInit() {

 this.toDateTransaction = this.shareData.toDateTransact;
 }

 searchByDate(data_fromDate, data_toDate,filterDataForSearch) {

   this.shareData.toDateTransact = this.dataToDate;

   this.toDateTransaction = this.shareData.toDateTransact;
  }
  }

错误 -

 Uncaught (in promise): Error: Template parse errors:
 Can't bind to 'options' since it isn't a known property of 'angular2- 
  date-picker'.


    <angular2-date-picker title="Click to enter From Date and Time" 
    [ERROR ->][options]="fromDateTransaction"
                [(ngModel)]="date" [settings]="settings" 
    "): ng:///MasterSystemModule/TransactionComponent.html@52:84

【问题讨论】:

    标签: angular datetimepicker


    【解决方案1】:

    date picker documentation 里面没有options property,

    所以使用,

    <angular2-date-picker title="Click to enter To Date and Time"  [settings]="settings1" [(ngModel)]="toDateTransaction">
    </angular2-date-picker>
    

    现在,要获取刷新数据,请将数据存储在 Localstorage

     ngOnInit() {
        if(localStorage.getItem('date'))
        {
          this.toDateTransaction = JSON.parse(localStorage.getItem('date'));
        }
    
      }
    
     searchByDate(data_fromDate, data_toDate,filterDataForSearch) {
    
       this.shareData.toDateTransact = this.dataToDate;
       this.toDateTransaction = this.shareData.toDateTransact;
       localStorage.setItem('date', JSON.stringify(this.toDateTransaction))
     }
    

    【讨论】:

    • 我不知道为什么,但不幸的是,这种方法不起作用。对此感到抱歉。当我在 init 上执行 console.log 时,它会在控制台中显示正确的值,但随后会刷新日期
    • 让我们调试一下,有什么问题吗?
    • 不...不,我没有遇到任何其他问题...除了这个
    • 请在您的模板中添加{{toDateTransaction}},我们会检查什么数据是cmng
    • 我这样做了 --> {{toDateTransaction}} ...数据是刷新的当前日期时间...。
    猜你喜欢
    • 1970-01-01
    • 2016-12-26
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    相关资源
    最近更新 更多