2015-02-06 32 views
0

我想将angularJS与来自此网站的datepicker相关联:http://freqdec.github.io/datePicker/ 根据文档,这是如何创建基于ID的datepicker,它只能由Id完成,而不是class,而不是jquery obj。angularJS与datepicker相关联(不是JQuery UI Datepicker,但别的东西)

datePickerController.createDatePicker({ 
formElements:{ 
    "inp1":"%d/%m/%Y" 
} 
}); 

我创建一个指令去做:

  .directive('datepicker', function() { 
      return { 
       restrict : 'A', // the directive is restricted to an attribute 
       require : 'ngModel', 
       link : function(scope, element, attrs, ngModelCtrl) { // the link() function is used to setup the datepicker 
        // http://freqdec.github.io/datePicker/ 
        var id = attrs.id; 
        var dateFormat = "%m/%d/%Y"; 
        var fe = {} 
        fe[id + ''] = dateFormat; 
        datePickerController.createDatePicker({ 
         // Associate the text input to a MM/DD/YYYY date format 
         formElements : fe 
        }); 
       } 
      } 
     }) 

当我调试,呼吁

datePickerController.createDatePicker({ 
         // Associate the text input to a MM/DD/YYYY date format 
         formElements : fe 
        }); 

之前,我看到

$(id) 

返回空。

[] 

然后我意识到,没有创建的HTML元素,因为我看到

element 

是一样的东西

<input class="col-xs-10 noPadding datepicker ng-pristine ng-untouched ng-valid" datepicker="" id="{{'licEffDate'+$index}}" ng-model="license.licEffDate"> 

在元素中的id尚未评估。

但我只能让这个ID准备好,以便将它传递到datepicker。

反正有解决这个问题吗?

+0

不同于论坛的网站,我们不使用的“谢谢”,或者“任何帮助表示赞赏”,或签名(因此)。请参阅“[应该'嗨','谢谢',标语和致敬从帖子中删除?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be BTW,它是“提前致谢”,而不是“Thanks in advanced”。 – 2015-02-07 05:17:22

回答

0

你需要做两件事情 transclude:true在指令和第二需要调用换到datepicker()$timeout推迟尝试,直到下一个角周期运行,确保translcuded ID在DOM设置。

Source

帮助Fiddle

+0

你节省了我的一天,非常感谢! – sh977218 2015-02-06 19:15:29

+0

伟大的快乐编码:-) – squiroid 2015-02-06 19:16:08