{"id":20854,"date":"2015-06-26T11:55:48","date_gmt":"2015-06-26T06:25:48","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=20854"},"modified":"2017-08-03T13:19:58","modified_gmt":"2017-08-03T07:49:58","slug":"immediate-and-non-immediate-model-updates-in-angularjs","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/immediate-and-non-immediate-model-updates-in-angularjs\/","title":{"rendered":"Immediate and non-immediate model updates in AngularJS"},"content":{"rendered":"<p>In <a title=\"Angular development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">AngularJS<\/a>, immediate model updates is a default behavior but if we want to persist value in the object after a certain time period has elapsed then we can achieve the same by using &#8220;debounce&#8221; object in <a href=\"http:\/\/www.tothenew.com\/blog\/explicitly-providing-ng-model-to-dom-elements\/\">&#8220;ng-model-options&#8221;<\/a> directive, which is known as &#8220;non-immediate model updates&#8221;.<\/p>\n<ol>\n<li>\n<h3>Immediate Model updates<\/h3>\n<p>[html]<br \/>\n&lt;div ng-app=&quot;MyApp&quot;&gt;<br \/>\n\t&lt;input type=&quot;text&quot; ng-model=&quot;message&quot;&gt;<br \/>\n\t&lt;p&gt;<br \/>\n\t\t{{ message }}<br \/>\n\t&lt;\/p&gt;<br \/>\n&lt;\/div&gt;<br \/>\n[\/html]<\/p>\n<p>In the example above the value of &#8220;message&#8221; immediately updates itself (<a title=\"Frontend Interfaces with AngularJS\" href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">default behavior of AngularJS<\/a>) on the change of value in the text box.<\/li>\n<li>\n<h3>Non-immediate model updates<\/h3>\n<p>You can use &#8220;debounce&#8221; to delay the model update\/validate with the &#8220;ng-model-options&#8221; directive.<\/p>\n<p>[html]<br \/>\n&lt;div ng-app=&quot;MyApp&quot;&gt;<br \/>\n\t&lt;input type=&quot;text&quot; ng-model=&quot;message&quot; ng-model-options=&quot;{ debounce : 1000 }&quot;&gt;<br \/>\n\t&lt;p&gt;<br \/>\n\t\t{{ message }}<br \/>\n\t&lt;\/p&gt;<br \/>\n&lt;\/div&gt;<br \/>\n[\/html]<\/p>\n<p>In the example above we added a directive &#8220;ng-model-options&#8221; in the textbox and then a key &#8220;debounce&#8221; in it with value &#8220;1000&#8221; ms. So now it will wait for 1 sec before updating its value according to the text in the box.<\/p>\n<p>We can also set custom debounce for each event using an object in &#8220;debounce&#8221;.<\/p>\n<p>[html]<br \/>\n&lt;div ng-app=&quot;MyApp&quot;&gt;<br \/>\n\t&lt;input type=&quot;text&quot; ng-model=&quot;message&quot; ng-model-options=&quot;{ updateOn: &#8216;default blur&#8217;, debounce : {default: 1000, blur: 0} }&quot;&gt;<br \/>\n\t&lt;p&gt;<br \/>\n\t\t{{ message }}<br \/>\n\t&lt;\/p&gt;<br \/>\n&lt;\/div&gt;<br \/>\n[\/html]<\/p>\n<p>In the example above value of &#8220;message&#8221; will update with a delay of 1 second from the change in the textbox or it will change immediately in case the textbox losses focus.<\/li>\n<\/ol>\n<p>Hope this helps !!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In AngularJS, immediate model updates is a default behavior but if we want to persist value in the object after a certain time period has elapsed then we can achieve the same by using &#8220;debounce&#8221; object in &#8220;ng-model-options&#8221; directive, which is known as &#8220;non-immediate model updates&#8221;. Immediate Model updates [html] &lt;div ng-app=&quot;MyApp&quot;&gt; &lt;input type=&quot;text&quot; ng-model=&quot;message&quot;&gt; [&hellip;]<\/p>\n","protected":false},"author":103,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":8},"categories":[1439,3429],"tags":[3955,955,4697,1857,1859,1858,1860],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/20854"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/103"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=20854"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/20854\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=20854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=20854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=20854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}