获取angular js中datepicker指令的值

get value of datepicker directive in angular js

本文关键字:指令 datepicker angular js 获取      更新时间:2023-09-26

这是我之前使用的代码…

<div class="date_menu" ng-repeat="singledate in dates">
    <p>
    {{singledate}}
    <input type="radio" name="filterDateChosen2" ng-model="$parent.radioResult" ng-value="singledate">
    </p>
</div></div>

以单选按钮的形式显示日期列表。这将所选日期保存在$parent.radioResult中,然后我可以使用…

访问它。
<button ng-click="getVar(radioResult)"...

但是现在,我想用jQuery的日期picker代替…

myApp.directive('calendar', function () {
    return function(scope, element, attrs) {
       element.datepicker({
           inline: true,
           dateFormat: 'dd-mm-yy',
           onSelect: function(dateText) {
               var modelPath = $(this).attr('ng-model');
               var val = $(this).attr('ng-value');
               putObject(modelPath, val, scope, dateText);
               scope.$apply();
           }
       });
   }
});

这个工作,如果我选择一个日期,输入框将显示它…

<input calendar ng-model="$parent.radioResult" />

但问题是,当我使用…

<button ng-click="getVar(radioResult)"...

它没有得到值,我不知道为什么?这是什么原因呢?

因为你使用的是一个不在Angular上下文中的JQuery Datepicker,所以你需要先把它带到Angular的上下文中,通过将选定的日期设置为作用域上的变量,就像下面

 $scope.$apply(function () {
    $scope.dateValue = selectedDate;
 });