{"id":12988,"date":"2014-04-15T18:51:17","date_gmt":"2014-04-15T13:21:17","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=12988"},"modified":"2017-08-03T16:01:32","modified_gmt":"2017-08-03T10:31:32","slug":"component-in-angularjs","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/component-in-angularjs\/","title":{"rendered":"Component in AngularJS"},"content":{"rendered":"<p>When we are working on front-end part, then many times we face a situation where same HTML repeats on the same page or different pages, at that time we think of making that HTML a component. When <a title=\"AngularJS Development Company\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">using AngularJS framework<\/a>, we can easily make <strong>components<\/strong> with the help of <strong>directives<\/strong>. Let&#8217;s quickly understand what a <strong>directive<\/strong> is?<br \/>\nA <a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-getting-started-with-directives\/\">directive<\/a> is a marker on DOM element, which tells <a title=\"Intuitive Frontend Interfaces with AngularJS\" href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">AngularJS to bind a specified behavior<\/a> to the DOM element and its children elements. <strong>AngularJS<\/strong> itself provides many built-in <strong>directives<\/strong> e.g. <strong>ngRepeat<\/strong>, <strong>ngShow<\/strong>, <strong>ngModel<\/strong> etc. <strong>AngularJS<\/strong> is case-sensitive while html is case-insensitive, so defining a <strong>directive<\/strong> with <strong>AngularJS<\/strong> and using a <strong>directive<\/strong> with html have a slightly different signature. Let&#8217;s see a very commonly used <strong>directive<\/strong> <strong>ngModel<\/strong>:<\/p>\n<p>[js]<\/p>\n<p>[\/js]<\/p>\n<p>We have marked <strong>ngModel<\/strong> <strong>directive<\/strong> to DOM element (input). We are using <strong>directive<\/strong> on html as &#8216;<strong>ng-model<\/strong>&#8216; (small letters with dash\/minus as delimiter for camelCase words) while its actually defined with <strong>AngularJS<\/strong> as <strong>ngModel<\/strong>. Now let&#8217;s create a simple custom <strong>directive<\/strong> (We define a <strong>directory<\/strong> to register it with <strong>module<\/strong> via <strong>module<\/strong>.<strong>directive<\/strong> API).<\/p>\n<p>[js]<br \/>\nvar directiveTestApp = angular.module(&#8216;directiveTest&#8217;, []);<br \/>\ndirectiveTestApp.directive(&#8216;myTemplate&#8217;, function () { \/\/ Registering myTemplate directive to directiveTest module<br \/>\n    return {<br \/>\n        \/*<br \/>\n         * Restricting a directive by providing a restrict field so that it can  be used as:<br \/>\n         * E stands for Element<br \/>\n         * C stand for Class name<br \/>\n         * M stands for Comment<br \/>\n         * A stands for Attribute<br \/>\n         * Here we are defining directive with all the types, if we don&#8217;t provide restrict field then default value is<br \/>\n         * &#8216;A&#8217; means directive can be used as attribute only.<br \/>\n         * *\/<br \/>\n        restrict: &quot;ECMA&quot;, \/\/ directive can be used as attribute, element, class or comment.<br \/>\n        replace: true, \/\/ replace indicates that we want to replace whole div or only inner HTML, default value is false.<br \/>\n        template: &quot;&lt;\/pre&gt;<br \/>\n&lt;div&gt;this is directive template test&lt;\/div&gt;<br \/>\n&lt;pre&gt;<br \/>\n&quot; \/\/ DOM element should be replaced with this.<br \/>\n        \/\/ If you want to provide template content from HTML file use templateUrl option instead of template<br \/>\n    };<br \/>\n});<br \/>\n[\/js]<\/p>\n<p>To define a <strong>directive<\/strong>, we use <strong>module<\/strong>.<strong>directive<\/strong> API, in which first argument is name of <strong>directive<\/strong>, and second argument is function which returns an object with all the options. This function is called at the time of initializing the <strong>directive<\/strong>. Therefore this function is also know as factory function. For the first time when <strong>AngularJS<\/strong> finds the use of <strong>directive<\/strong>, it calls this factory function and stores the results for future use.<\/p>\n<p>We can use <strong>directive<\/strong> on html as attribute, tag element, class name and comment as below:<\/p>\n<p>[js]<br \/>\n1. Attribute Directive:&lt;\/pre&gt;<br \/>\n&lt;div&gt;&lt;\/div&gt;<\/p>\n<p>&lt;hr \/&gt;<\/p>\n<p>&lt;pre&gt;2. Element Directive:&lt;\/pre&gt;<\/p>\n<p>&lt;hr \/&gt;<\/p>\n<p>&lt;pre&gt;3. Class Directive:&lt;\/pre&gt;<br \/>\n&lt;div class=&quot;my-template&quot;&gt;&lt;\/div&gt;<\/p>\n<p>&lt;hr \/&gt;<\/p>\n<p>&lt;pre&gt;4. Comment Directive: &lt;!&#8211; directive: my-template hello &#8211;&gt;&lt;\/pre&gt;<\/p>\n<p>&lt;hr \/&gt;<\/p>\n<p>&lt;pre&gt;[\/js]<\/p>\n<p><strong>NOTE<\/strong>: You can checkout full working source code from this <a href=\"https:\/\/github.com\/AmitThakkar\/AngularDemo\" target=\"_blank\">link<\/a>.<br \/>\n<strong><a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit-kumar\/\">More Blogs by Me<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we are working on front-end part, then many times we face a situation where same HTML repeats on the same page or different pages, at that time we think of making that HTML a component. When using AngularJS framework, we can easily make components with the help of directives. Let&#8217;s quickly understand what a [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1185],"tags":[3955,955,4697,1387,1189,1388],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12988"}],"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\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=12988"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12988\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=12988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=12988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=12988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}