admin管理员组文章数量:1333210
I'm trying to convert jQuery plugin into directive. Here is the library: Github.
In the documentation there is an option :
$(document).ready(function() {
$("#datepicker").datepicker();
$("#datepickerbtn").click(function(event) {
event.preventDefault();
$("#datepicker").focus();
})
});
Directive I've created :
app.directive('dateP', function(){
return{
restrict:'A',
require:'ngModel',
link:function(scope, element, attr, ngModel){
$(element).datepicker(scope.$eval(attr.dateP));
console.log('hey');
ngModel.$setViewValue(scope);
}
}
});
but it's not working , any help would be appreciate it .
Plunker .
I've read this : /
I'm trying to convert jQuery plugin into directive. Here is the library: Github.
In the documentation there is an option :
$(document).ready(function() {
$("#datepicker").datepicker();
$("#datepickerbtn").click(function(event) {
event.preventDefault();
$("#datepicker").focus();
})
});
Directive I've created :
app.directive('dateP', function(){
return{
restrict:'A',
require:'ngModel',
link:function(scope, element, attr, ngModel){
$(element).datepicker(scope.$eval(attr.dateP));
console.log('hey');
ngModel.$setViewValue(scope);
}
}
});
but it's not working , any help would be appreciate it .
Plunker .
I've read this : https://amitgharat.wordpress./2013/02/03/an-approach-to-use-jquery-plugins-with-angularjs/
Share Improve this question edited May 1, 2016 at 15:37 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 20, 2015 at 13:31 SadeghbayanSadeghbayan 1,1632 gold badges19 silver badges38 bronze badges1 Answer
Reset to default 6Basically you written ng-mode
instead of ng-model
and directive you should define date-picker options not the scope.$eval(attr.dateP)
which is totally wrong. Inside datepicker you need to provide their options in json
format like here we mentioned option as { format: 'dd/mm/yyyy' })
HTML
<input date-p id="datepicker1" class="input-small" type="text" ng-model="dt">
Directive
app.directive('dateP', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ngModel) {
element.datepicker({
format: 'dd/mm/yyyy'
});
}
}
});
Update
For show datepicker
on button click you need to do add below method inside your controller.
Controller
$scope.showDatepicker = function(){
angular.element('#datepicker1btn').datepicker('show');
};
Working Plunkr
Thanks.
本文标签: javascriptConvert jquery plugin into directive angularStack Overflow
版权声明:本文标题:javascript - Convert jquery plugin into directive angular - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742347651a2457849.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论