Can't set datepicker date from controller in AngularJS

喜你入骨 提交于 2021-02-07 21:36:21

问题


I am trying to create an AngularJS datepicker on the push of a button. I am using this bootstrap-ui control. The control works (pops up on the click of a button, and I can select a date), but I can't seem to set the initial date back from the scope (so that when it is first opened, the designated date is already selected).

The error I get in the Chrome console is:

Datepicker directive: "ng-model" value must be a Date object, a number of milliseconds since 01.01.1970 or a string representing an RFC2822 or ISO 8601 date.

jade:

button(type="button" ng-model="date.from" btn-radio="'From'" ng-click="date.openFromPopup($event);" show-weeks="false" show-button-bar="false" datepicker-popup = "date.format" is-open = "date.fromOpened" min = "date.minDate" max = "date.today") From

Since I have a watch on $scope.date.from, I can confirm that the selected date is correct, but I can never set it from the controller. Any ideas?

AngularJS controller:

$scope.date = {};

$scope.date.format = "yyyy/MM/dd";
$scope.date.opened = false;
$scope.date.from = new Date();
$scope.date.today = new Date();
$scope.date.minDate = null;
$scope.date.fromOpened = false;

$scope.$watch('date.from', function(v) {
if(v){
     alert(v);
}
});

回答1:


In the example on the angular-ui page, it says to use it like this...

$scope.from = function() {
    $scope.dt = new Date();
};
$scope.from();

Does that work?



来源:https://stackoverflow.com/questions/21341806/cant-set-datepicker-date-from-controller-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!