如何从jquery更新控制器变量?
我有一个日期选择器小部件,我想将选择的日期传递给控制器。 我在我的视图中有这个脚本片段:
$('#datepicker').datepicker(); $('#datepicker').datepicker() .on('changeDate', function(ev){ alert(ev.date); });
如何将“ev.date”发送给控制器?
我希望它影响控制器变量,然后通过指令拾取此更改并在指令中修改dom。
你可以这样做:
$('#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