如何从jQuery更新控制器变量? [英] How to update controller variable from jquery?

查看:72
本文介绍了如何从jQuery更新控制器变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期选择器小部件,我想将选择的日期传递给控制器​​.我的视图中有以下脚本片段:

I have a date picker widget, and I want to pass the picked date to the controller. I have this script snippet in my view:

        <div id="datepicker"></div>
        <script type="text/javascript">
        $('#datepicker').datepicker();
        $('#datepicker').datepicker()
        .on('changeDate', function(ev){

          alert(ev.date);
        });
        </script>

如何将此"ev.date"发送给控制器?

How to send this "ev.date" to the controller?

我希望它影响控制器变量,因此此更改随后将由指令提取并在指令中修改dom.

I want it to affect controller variable so this change is then picked up by a directive and dom is modified in the directive.

推荐答案

您可以执行以下操作:

$('#datepicker').datepicker()
  .on('changeDate', function(ev){
     //get a hold of controller and scope
     var element = angular.element($('#datepicker'));
     var controller = element.controller();
     var scope = element.scope();

     //as this happends outside of angular you probably have to notify angular of the change by wrapping your function call in $apply
     scope.$apply(function(){
       scope.updateDateFunction(ev.date);
     });
 });

请参阅: http://docs.angularjs.org/api/angular.element

这篇关于如何从jQuery更新控制器变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆