【问题标题】:angularJS associated with datepicker (not JQuery UI Datepicker but something else)与 datepicker 关联的 angularJS(不是 JQuery UI Datepicker,而是其他东西)
【发布时间】:2015-02-06 18:54:03
【问题描述】:

我正在尝试将 angularJS 与来自该站点的 datepicker 相关联:http://freqdec.github.io/datePicker/ 根据文档,这就是它基于Id创建日期选择器的方式,它只能由Id完成,而不是类,而不是jquery obj。

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 以便将它传递给日期选择器。

有没有办法解决这个问题?

【问题讨论】:

标签: jquery angularjs datepicker


【解决方案1】:

你需要做两件事 transclude:true in 指令和第二个需要将对 datepicker() 的调用包装在 $timeout 中以延迟尝试直到下一个角循环运行,确保在 DOM 中设置转换后的 ID。

Source

求助Fiddle

【讨论】:

  • 编码愉快:-)
猜你喜欢
  • 2014-10-22
  • 2010-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-12
  • 2010-11-26
相关资源
最近更新 更多