2014-08-28 77 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(); 
       } 
      }