带有 Angularjs 的 jQuery ui 日期选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18144142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery ui datepicker with Angularjs
提问by user2473037
I want to use jQuery UI datepicker with AngularJS.
我想在 AngularJS 中使用 jQuery UI datepicker。
I have a sample , but my code is not working.
我有一个示例,但我的代码不起作用。
Sample:
样本:
http://www.abequar.net/jquery-ui-datepicker-with-angularjs/
http://www.abequar.net/jquery-ui-datepicker-with-angularjs/
My Code:
我的代码:
<input id="sDate" name="programStartDate" type="text" datepicker required/>
angular.module('elnApp')
.directive('datepicker', function () {
return {
restrict: 'A',
require : 'ngModel',
link : function (scope, element, attrs, ngModelCtrl) {
$(function(){
element.datepicker({
dateFormat:'yy-mm-dd',
onSelect:function (date) {
ngModelCtrl.$setViewValue(date);
scope.$apply();
}
});
});
}
} });
It shows an error TypeError: Object [object Object] has no method 'datepicker'
.
它显示一个错误TypeError: Object [object Object] has no method 'datepicker'
。
回答by Kevin Jones
I have almost exactly the same code as you and mine works.
我的代码与您和我的作品几乎完全相同。
Do you have jQueryUI.js included in the page?
页面中是否包含 jQueryUI.js?
There's a fiddle here
有一个小提琴在这里
<input type="text" ng-model="date" jqdatepicker />
<br/>
{{ date }}
var datePicker = angular.module('app', []);
datePicker.directive('jqdatepicker', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
element.datepicker({
dateFormat: 'DD, d MM, yy',
onSelect: function (date) {
scope.date = date;
scope.$apply();
}
});
}
};
});
You'll also need the ng-app="app" somewhere in your HTML
您还需要在 HTML 中的某处使用 ng-app="app"
回答by vicont
angular.module('elnApp')
.directive('jqdatepicker', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
$(element).datepicker({
dateFormat: 'dd.mm.yy',
onSelect: function(date) {
ctrl.$setViewValue(date);
ctrl.$render();
scope.$apply();
}
});
}
};
});
回答by Nahn
As a best practice, especially if you have multiple date pickers, you should not hardcode the element's scope variable name.
作为最佳实践,尤其是当您有多个日期选择器时,您不应硬编码元素的范围变量名称。
Instead, you should get the clicked input's ng-model
and update its corresponding scope variable inside the onSelect
method.
相反,您应该获取点击的输入ng-model
并在onSelect
方法内更新其相应的范围变量。
app.directive('jqdatepicker', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, ngModelCtrl) {
$(element).datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function(date) {
var ngModelName = this.attributes['ng-model'].value;
// if value for the specified ngModel is a property of
// another object on the scope
if (ngModelName.indexOf(".") != -1) {
var objAttributes = ngModelName.split(".");
var lastAttribute = objAttributes.pop();
var partialObjString = objAttributes.join(".");
var partialObj = eval("scope." + partialObjString);
partialObj[lastAttribute] = date;
}
// if value for the specified ngModel is directly on the scope
else {
scope[ngModelName] = date;
}
scope.$apply();
}
});
}
};
});
EDIT
编辑
To address the issue that @Romain raised up (Nested Elements), I have modified my answer
为了解决@Romain 提出的问题(嵌套元素),我修改了我的答案
回答by rohit garg
I finally was able to run datepicker directive in angular js , here are pointers
我终于能够在 angular js 中运行 datepicker 指令,这里是指针
include following JS in order
按顺序包括以下JS
- jquery.js
- jquery-ui.js
- angular-min.js
- jquery.js
- jquery-ui.js
- angular-min.js
I added the following
我添加了以下内容
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"> </script>
in html code
在 html 代码中
<body ng-app="myApp" ng-controller="myController">
// some other html code
<input type="text" ng-model="date" mydatepicker />
<br/>
{{ date }}
//some other html code
</body>
in the js , make sure you code for the directive first and after that add the code for controller , else it will cause issues.
在 js 中,请确保先为指令编码,然后为控制器添加代码,否则会导致问题。
date picker directive :
日期选择器指令:
var app = angular.module('myApp',[]);
app.directive('mydatepicker', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
element.datepicker({
dateFormat: 'DD, d MM, yy',
onSelect: function (date) {
scope.date = date;
scope.$apply();
}
});
}
};
});
directive code referred from above answers.
上述答案中引用的指令代码。
After this directive , write the controller
在此指令之后,编写控制器
app.controller('myController',function($scope){
//controller code
};
TRY THIS INSTEADin angular js
在 angular js 中试试这个
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
along with jquery.js and jquery-ui.js
连同 jquery.js 和 jquery-ui.js
we can implement angular js datepicker as
我们可以将 angular js datepicker 实现为
<input type="date" ng-model="date" name="DOB">
This gives the built in datepicker and date is set in ng-model and can be used for further processing and validation.
这提供了内置的日期选择器,日期设置在 ng-model 中,可用于进一步处理和验证。
Found this after lot of successful headbanging with previous approach. :)
在使用以前的方法成功地进行了多次碰撞后发现了这一点。:)
回答by zeillah
Unfortunately, vicont's answer did not work for me, so I searched for another solution which is as elegant and works for nested attributes in the ng-model as well. It uses $parse and accesses the ng-model through the attrs in the linking function instead of requiring it:
不幸的是,vicont 的答案对我不起作用,所以我搜索了另一个同样优雅的解决方案,它也适用于 ng-model 中的嵌套属性。它使用 $parse 并通过链接函数中的 attrs 访问 ng-model 而不是要求它:
myApp.directive('myDatepicker', function ($parse) {
return function (scope, element, attrs, controller) {
var ngModel = $parse(attrs.ngModel);
$(function(){
element.datepicker({
...
onSelect:function (dateText, inst) {
scope.$apply(function(scope){
// Change binded variable
ngModel.assign(scope, dateText);
});
}
});
});
}
});
Source: ANGULAR.JS BINDING TO JQUERY UI (DATEPICKER EXAMPLE)
回答by hS4
I modified the code and wrapped view update inside $apply().
我修改了代码并将视图更新封装在 $apply() 中。
link: function (scope, elem, attrs, ngModelCtrl){
var updateModel = function(dateText){
// call $apply to update the model
scope.$apply(function(){
ngModelCtrl.$setViewValue(dateText);
});
};
var options = {
dateFormat: "dd/mm/yy",
// handle jquery date change
onSelect: function(dateText){
updateModel(dateText);
}
};
// jqueryfy the element
elem.datepicker(options);
}
}
working fiddle - http://jsfiddle.net/hsfid/SrDV2/1/embedded/result/
回答by Jonghee Park
onSelect doesn't work well in ng-repeat, so I made another version using event bind
onSelect 在 ng-repeat 中效果不佳,所以我使用事件绑定制作了另一个版本
html
html
<tr ng-repeat="product in products">
<td>
<input type="text" ng-model="product.startDate" class="form-control date-picker" data-date-format="yyyy-mm-dd" datepicker/>
</td>
</tr>
script
脚本
angular.module('app', []).directive('datepicker', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
element.datepicker();
element.bind('blur keyup change', function(){
var model = attrs.ngModel;
if (model.indexOf(".") > -1) scope[model.replace(/\.[^.]*/, "")][model.replace(/[^.]*\./, "")] = element.val();
else scope[model] = element.val();
});
}
};
});
回答by Ciaran Whyte
You main Index.html file for Angular can use the body tag as the ng-view. Then all you need to do is include a script tag at the bottom of whatever page is being pulled into Index.html by Angular like so:
Angular 的主要 Index.html 文件可以使用 body 标签作为 ng-view。然后你需要做的就是在任何页面被 Angular 拉入 Index.html 的底部包含一个脚本标签,如下所示:
<script type="text/javascript">
$( function() {
$( "#mydatepickerid" ).datepicker({changeMonth: true, changeYear: true,
yearRange: '1930:'+new Date().getFullYear(), dateFormat: "yy-mm-dd"});
});
</script>
Why overcomplicate things??
为什么要把事情复杂化??
回答by user8237545
var selectDate = element.datepicker({
dateFormat:'dd/mm/yy',
onSelect:function (date) {
ngModelCtrl.$setViewValue(date);
scope.$apply();
}
}).on('changeDate', function(ev) {
selectDate.hide();
ngModelCtrl.$setViewValue(element.val());
scope.$apply();
});
回答by Amit Bhandari
myModule.directive('jqdatepicker', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
element.datepicker({
dateFormat: 'dd/mm/yy',
onSelect: function (date) {
var ar=date.split("/");
date=new Date(ar[2]+"-"+ar[1]+"-"+ar[0]);
ngModelCtrl.$setViewValue(date.getTime());
// scope.course.launchDate = date;
scope.$apply();
}
});
}
};
});
HTML Code :
HTML代码:
<input type="text" jqdatepicker ng-model="course.launchDate" required readonly />