首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在bootstrap-daterangepicker日期更改时更新绑定?

如何在bootstrap-daterangepicker日期更改时更新绑定?
EN

Stack Overflow用户
提问于 2013-10-27 04:45:11
回答 2查看 452关注 0票数 0

我在我的AngularJS应用程序中使用this plugin和变量来存储开始和结束日期,无论它们何时改变。

在日期范围更改时,如何调用控制器方法来更新页面上的数据?

比如调用作用域的这个方法:

代码语言:javascript
复制
$scope.updateDataOnDateRangeChange = function(){
    // here calling few services to fetch new data for the page
    // for the selected date range
}

如何使用此插件的ng-click或ng-change或自定义指令?

EN

回答 2

Stack Overflow用户

发布于 2013-10-27 11:33:52

使用jQuery插件的基本步骤通常是为它创建一个指令。在link回调中,您可以初始化插件。在这里,您可以访问作为jQuery或jQ-lite对象的元素、角度作用域和插件回调。在第三方代码中更改作用域时,请参阅有关使用$.apply()的重要注释

示例html:

代码语言:javascript
复制
<input my-picker type="text"/>

基本脚本大纲:

代码语言:javascript
复制
app.directive('myPicker', function ($timeout) {
    return {
        link: function (scope, elem, attrs) {
            $timeout(function () {
                /* elem is a jQuery or jQ-lite object representing the element*/
                elem.daterangepicker({
                    format: 'YYYY-MM-DD',
                    startDate: '2013-01-01',
                    endDate: '2013-12-31'
                },
               /* not overly familiar with this plugin but this callback 
                 should allow you to set angular scopes */
                function (start, end) {
                    /* important to do any scope changes within `$.apply()` so angular
                      becomes aware of changes from third party code*/
                    scope.$apply(function(){
                        /* do what you need here with angular scope
                         have access to plugin data as well as angular scope*/

                    })
                });
            }, 1)
        }
    }

})

您可以通过向标记添加其他属性来增强此指令,然后使用这些属性来设置各种插件选项。

票数 1
EN

Stack Overflow用户

发布于 2013-10-27 04:48:32

AngularJS中存在一个ngChange directive,它应该执行您所描述的操作。

将此指令添加到日期范围输入元素。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19611710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档