Sad*_*yan 5 javascript jquery datepicker angularjs angularjs-directive
我正在尝试将jQuery插件转换为指令.这是图书馆:Github.
在文档中有一个选项:
$(document).ready(function() {
        $("#datepicker").datepicker();
        $("#datepickerbtn").click(function(event) {
            event.preventDefault();
            $("#datepicker").focus();
        })
    });
我创建的指令:
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);
        }
    }
}); 
但它不起作用,任何帮助都会欣赏它.
我读过这个:https://amitgharat.wordpress.com/2013/02/03/an-approach-to-use-jquery-plugins-with-angularjs/
基本上你写的ng-mode不是ng-model和指令你应该定义日期选择器选项而不是scope.$eval(attr.dateP)完全错误.在datepicker里面你需要提供他们的选项,json就像我们提到的选项一样{ format: 'dd/mm/yyyy' })
HTML
<input date-p id="datepicker1" class="input-small" type="text" ng-model="dt">
指示
app.directive('dateP', function() {
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function(scope, element, attr, ngModel) {
      element.datepicker({
        format: 'dd/mm/yyyy'
      });
    }
  }
});
更新
对于datepicker按钮单击显示,您需要在控制器内添加以下方法.
调节器
$scope.showDatepicker =  function(){
  angular.element('#datepicker1btn').datepicker('show');
};
谢谢.
| 归档时间: | 
 | 
| 查看次数: | 5342 次 | 
| 最近记录: |