{"id":10226,"date":"2013-04-25T17:06:57","date_gmt":"2013-04-25T11:36:57","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=10226"},"modified":"2017-08-03T16:04:05","modified_gmt":"2017-08-03T10:34:05","slug":"angularjs-getting-started-with-directives","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/angularjs-getting-started-with-directives\/","title":{"rendered":"AngularJS : Getting started with Directives"},"content":{"rendered":"<p style=\"text-align: justify;\">In this post we will go through an important aspect of <a title=\"AngularJS Development Company\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">AngularJS<\/a> i.e., <strong>Directives<\/strong>.<\/p>\n<p style=\"text-align: justify;\"><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-getting-started-with-directives\/\">Directives<\/a> helps us do things in a better and cleaner way. Lets get into the code rather going into\u00a0theoretical\u00a0explanations, which will make it more clear what actually directives do.<\/p>\n<p style=\"text-align: justify;\">We have written a simple directive below which shows some HTML when the element being created is encountered.<\/p>\n<p>[html]<\/p>\n<p>&lt;script src=&quot;https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.0.1\/angular.min.js&quot; type=&quot;text\/javascript&quot;&gt;&lt;\/script&gt;<\/p>\n<p>&lt;\/pre&gt;<br \/>\n&lt;div&gt;&lt;\/div&gt;<br \/>\n&lt;pre&gt;<br \/>\n&lt;script&gt;\/\/ &lt;![CDATA[<\/p>\n<p>myApp = angular.module(&quot;FirstDirective&quot;,[]);<\/p>\n<p>myApp.directive(&#8216;mytag&#8217;,function(){<br \/>\n    return {<br \/>\n        restrict: &quot;E&quot;,<br \/>\n        template: &quot;&lt;strong&gt;This is a great First Directive&lt;\/strong&gt;&quot;,<br \/>\n        replace: true,<br \/>\n    }<br \/>\n})<\/p>\n<p>\/\/ ]]&gt;&lt;\/script&gt;<\/p>\n<p>[\/html]<\/p>\n<p style=\"text-align: justify;\">In the above code, we have &#8220;mytag&#8221; element which when encountered in HTML renders the corresponding template. \u00a0Lets see what the code does,<\/p>\n<p style=\"text-align: justify;\"><strong>myApp.directive()<\/strong> is another syntax that we have to remember. The first argument in this is the name of the tag that you want, in our case its <strong>&#8220;mytag&#8221;<\/strong>. Second thing is a factory function which returns an object that defines the directive properties.<\/p>\n<p style=\"text-align: justify;\">In this object we have set <strong>&#8220;restrict&#8221;<\/strong> as E which makes it an Element that can be used like any of HTML tags. The second option <strong>&#8220;template&#8221;<\/strong> defines the HTML template to be loaded and the third option <strong>&#8220;replace&#8221;<\/strong> if set to true replaces the current element with the template.<\/p>\n<p style=\"text-align: justify;\">We will learn more about other options available to us in the next post.<\/p>\n<p style=\"text-align: justify;\">Hope it Helps! Feel free to ask if you have any queries.<\/p>\n<p>Read Further on AngularJS Series<\/p>\n<ul>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-a-write-less-do-more-javascript-framework\/\">AngularJS :\u00a0A \u201cWrite Less Do More\u201d JavaScript Framework<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-updating-a-label-dynamically-with-user-input\/\">AngularJS :\u00a0Updating a Label Dynamically with User Input<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-adding-items-to-a-javascript-list-and-updating-corresponding-dom-dynamically\/\">AngularJS :\u00a0Adding Items to a JavaScript List and updating corresponding DOM Dynamically<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-text-suggestions\/\">AngularJS :\u00a0Text Suggestions<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-sorting-objects-on-various-attributes\/\">AngularJS :\u00a0Sorting objects on various attributes<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-fetching-data-from-the-server\/\">AngularJS :\u00a0Fetching data from the server<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-multiple-views-layout-template-and-routing\/\">AngularJS :\u00a0Multiple Views, Layout\u00a0Template\u00a0and Routing<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-implementing-routes-and-multiple-views\/\">AngularJS :\u00a0Implementing Routes And Multiple Views<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Building Intuitive Frontend Interfaces with AngularJS<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this post we will go through an important aspect of AngularJS i.e., Directives. Directives helps us do things in a better and cleaner way. Lets get into the code rather going into\u00a0theoretical\u00a0explanations, which will make it more clear what actually directives do. We have written a simple directive below which shows some HTML when [&hellip;]<\/p>\n","protected":false},"author":63,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1439,3429],"tags":[3955,955,4697,1155],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/10226"}],"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\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=10226"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/10226\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=10226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=10226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=10226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}