如果您使用的是AngularJS 1.x版本,请尝试使用以下代码来手动更新日期选择器:
$scope.$watch('date', function(newVal, oldVal) { if (newVal !== oldVal) { $timeout(function() { $('.datepicker').datepicker('update'); }); } });
如果您使用的是Angular 2+版本,请尝试使用以下代码:
import { Component, ViewChild } from '@angular/core'; import { NgbDatepicker } from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'app-root',
template:
})
export class AppComponent {
@ViewChild('dp') dp: NgbDatepicker;
date = { year: 2017, month: 6, day: 6 };
changeDate() { this.date = { year: 2018, month: 8, day: 8 }; setTimeout(() => { this.dp.navigateTo({ year: this.date.year, month: this.date.month }); }, 1); } }
这将强制日期选择器导航到当前日期。请注意,您需要安装ng-bootstrap模块才能使用NgbDatepicker组件。