{"id":15036,"date":"2014-07-28T11:15:42","date_gmt":"2014-07-28T05:45:42","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=15036"},"modified":"2014-07-28T11:15:42","modified_gmt":"2014-07-28T05:45:42","slug":"explicitly-providing-ng-model-to-dom-elements","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/explicitly-providing-ng-model-to-dom-elements\/","title":{"rendered":"Explicitly providing ng-model to DOM elements"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/12\/angularjs.jpg\" alt=\"angularjs\" width=\"600\" height=\"300\" class=\"aligncenter size-full wp-image-14740\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2014\/12\/angularjs.jpg 600w, \/blog\/wp-ttn-blog\/uploads\/2014\/12\/angularjs-300x150.jpg 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>In AngularJS, we can easily write our own custom directives to achieve required functionality. There are certain attributes which we provide in directive definition like <code>restrict<\/code> , <code>scope<\/code> , <code>template<\/code>, <code>link<\/code>, <code>transclude<\/code> etc.<\/p>\n<p>There is one more attribute named : <code>require<\/code> , which gives us the liberty to include another directive and inject its controller as the fourth argument to the linking function. It accept both string as well as array type. In case of array, the controllers are included in the given order. There are some prefixes, when applied before controller name, provides different functionality:<\/p>\n<ul>\n<li><strong>1. Without prefix<\/strong> &#8211; Locate the required controller on the current element. Throw an error if not found.<\/li>\n<li><strong>2. (?)<\/strong> &#8211; Attempt to locate the required controller or pass null to the link function if not found.<\/li>\n<li><strong>3. (^)<\/strong> &#8211; Locate the required controller by searching the element&#8217;s parents. Throw an error if not found.<\/li>\n<li><strong>4. (?^)<\/strong> &#8211; Attempt to locate the required controller by searching the element&#8217;s parents or pass null to the link function if not found.<\/li>\n<\/ul>\n<p><\/p>\n<p>Lets understand it with an example. We have a <code>&lt;div&gt;<\/code> and we want to assign ng-model to it which is not provided in angular by default. So, we write a custom directive for it and implement the required task inside it.<br \/>\n[js]<br \/>\nangular.module(&#8216;demoApp&#8217;, []).directive(&#8216;contenteditable&#8217;, function() {<br \/>\n    return {<br \/>\n      restrict: &#8216;A&#8217;,<br \/>\n      require: &#8216;?ngModel&#8217;,<br \/>\n      link: function(scope, element, attrs, ngModelCntrl) {<br \/>\n        if(!ngModel) return;<\/p>\n<p>        ngModel.$render = function() {<br \/>\n          element.html(ngModel.$viewValue || &#8221;);<br \/>\n        };<\/p>\n<p>        element.bind(&#8216;keyup&#8217;, function() {<br \/>\n          scope.$apply(write);<br \/>\n        });<br \/>\n        write();<\/p>\n<p>        function write() {<br \/>\n          ngModel.$setViewValue(element.html());<br \/>\n        }<br \/>\n      }<br \/>\n    };<br \/>\n});<br \/>\n[\/js]<br \/>\nThe html file will use this directive like:<br \/>\n[js]<br \/>\n&lt;div id=&quot;contentBox&quot; style=&#8217;height: 55px&#8217;&gt;<\/p>\n<p>&lt;\/div&gt;<br \/>\n[\/js]<\/p>\n<p>Here, we provide <code>ngModel<\/code> in require attribute and therefore we get <code>ngModelCntrl<\/code> as fourth augument which is an instance of the required <code>NgModelController<\/code>( built-in controller to handle setting our model value).<\/p>\n<p>There are various functions related to NgModelController like <code>$viewValue()<\/code>, <code>$modelValue()<\/code>, <code>$setViewValue()<\/code> etc, which help us to play with it.<\/p>\n<p>So, in this way we can explicitly provide ng-model to those DOM elements for which angular donot implicitly provide ng-model like all input elements.<\/p>\n<p>Hope it helps \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Directives there is one more attribute named : &#8220;require&#8221; , which gives us the liberty to include another directive and inject its controller as the fourth argument to the linking function. It accept both string as well as array type. In case of array, the controllers are included in the given order. There are some prefixes, when applied before controller name, provides different functionality.<\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[1],"tags":[955,1155,55,1492,1493],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/15036"}],"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\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=15036"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/15036\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=15036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=15036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=15036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}