2014-08-28 56 views
0

我已经创建了jQuery小部件,它基本上向本机jQuery UI Datepicker添加了更多功能和CSS样式。我使用日期选择器的onSelect选项来相应地更新<input>。问题是,即使输入发生变化,$ scope的模型也不会改变。我知道我需要使用$ scope。$ apply()以某种方式通知角度输入已经改变。

我尝试了以下,但它没有奏效。

this.options.onSelect = function (dateText, inst) { 


       $el.val(dateText); 
       that.$scope.$apply(); // This isn't working. I think I need to do something else 
       that.wrap.hide(); 


       jQuery(document).find('.iconalt').hide(); 


       if (that.options.afterSelect !== undefined) { 
        that.options.afterSelect(); 
       } 
      } 
+0

这已经是一个非常普遍的问题...正确吗?没有人知道?我做错了吗? – 2014-08-28 18:35:41

+1

如果您使用'$ el.val(dateText);'设置了值,那么在这种情况下,您的ngModel不会改变,甚至不需要'。scope。$ apply();'。我想你需要更新模型,然后做scope.apply() – PSL 2014-08-28 18:37:12

+0

如果你真的需要修改DOM,创建一个自定义的角度指令。另外:http://stackoverflow.com/questions/14994391/how-do-i-think-in-angularjs-if-i-have-a-jquery-background – mb21 2014-08-28 18:39:35

回答

0

我能找到解决方案。我需要更新模型之前调用$ apply()

this.options.onSelect = function (dateText, inst) { 


       $el.val(dateText); 
       that.$scope.date = dateText; // Adding this worked 
       that.$scope.$apply(); 
       that.wrap.hide(); 


       jQuery(document).find('.iconalt').hide(); 


       if (that.options.afterSelect !== undefined) { 
        that.options.afterSelect(); 
       } 
      } 
相关问题