在angular js中获取datepicker指令的值(get value of datepicker directive in angular js)

编程入门 行业动态 更新时间:2024-10-24 10:16:48
在angular js中获取datepicker指令的值(get value of datepicker directive in angular js)

这是我之前使用的代码......

<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 datepicker代替......

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)"...

它没有得到价值,我无法弄清楚为什么? 这是什么原因?

This was the code I was using before...

<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>

This displayed a list of dates in the form of radio buttons. This saved the selected date in $parent.radioResult where I was able to then access it with...

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

But now, I want to use jQuery datepicker instead...

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(); } }); } });

This works, if I select a date, the input box will show it...

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

But the problem is, when I use...

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

It does not get the value, and I cannot figure out why? What is the reason for this?

最满意答案

由于您使用的是不在Angular上下文中的JQuery Datepicker,因此您需要首先将所选日期设置为范围内的变量,如下所示将其置于Angular的上下文中

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

Since you are using a JQuery Datepicker which is not in the Angular context so you need to bring it into the Angular's context first by setting the selected date to the variable on the scope like below

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

更多推荐

本文发布于:2023-07-24 01:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240102.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指令   js   angular   directive   datepicker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!