{"id":68351,"date":"2021-08-01T10:44:30","date_gmt":"2021-08-01T14:44:30","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/building-your-own-directives-in-angularjs-angularjs-by-sayed-ahmed\/"},"modified":"2021-08-01T10:44:30","modified_gmt":"2021-08-01T14:44:30","slug":"building-your-own-directives-in-angularjs-angularjs-by-sayed-ahmed","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=68351","title":{"rendered":"Building Your Own Directives in AngularJS #AngularJS #By Sayed Ahmed"},"content":{"rendered":"<p>Building Your Own Directives<br \/>\nDirectives can appear as HTML elements, attributes, comments, or CSS classes. Examples<br \/>\n&lt;my-directive&gt;&lt;\/my-directive&gt;<br \/>\n&lt;input my-directive&gt;<br \/>\n&lt;!&#8211; directive: my-directive&#8211;&gt;<br \/>\n&lt;input&gt;<\/p>\n<p>Defining a Directive<br \/>\nangular.module(&#8216;app&#8217;, []).directive(&#8216;myDir&#8217;, function() {<br \/>\nreturn myDirectiveDefinition;<br \/>\n});<\/p>\n<p>Writing a Button Directive<br \/>\nescribe(&#8216;button directive&#8217;, function () {<br \/>\nvar $compile, $rootScope;<br \/>\nbeforeEach(module(&#8216;directives.button&#8217;));<br \/>\nbeforeEach(inject(function(_$compile_, _$rootScope_) {<br \/>\n$compile = _$compile_;<br \/>\n$rootScope = _$rootScope_;<br \/>\n}));<\/p>\n<p>it(&#8216;adds a &#8220;btn&#8221; class to the button element&#8217;, function() {<br \/>\nvar element = $compile(&#8216;&lt;button&gt;&lt;\/button&gt;&#8217;)($rootScope);<br \/>\nexpect(element.hasClass(&#8216;btn&#8217;)).toBe(true);<br \/>\n});<br \/>\n});<br \/>\n});<\/p>\n<p>Using the button directive<br \/>\n&lt;button type=&#8221;submit&#8221;<br \/>\n&gt;Click Me!&lt;\/button&gt;<\/p>\n<p>Implementing a Custom Validation Directives<br \/>\nImplementing a custom validation directive<\/p>\n<p>Now we have our tests in place, so we can implement the functionality of the directive:<\/p>\n<p>myModule.directive(&#8216;validateEquals&#8217;, function() {<br \/>\nreturn {<br \/>\nrequire: &#8216;ngModel&#8217;,<br \/>\nlink: function(scope, elm, attrs, ngModelCtrl) {<br \/>\nfunction validateEqual(myValue) {<br \/>\nvar valid= (myValue === scope.$eval(attrs.validateEquals));<br \/>\nngModelCtrl.$setValidity(&#8216;equal&#8217;, valid);<br \/>\nreturn valid ? myValue : undefined;<br \/>\n}<\/p>\n<p>ngModelCtrl.$parsers.push(validateEqual);<br \/>\nngModelCtrl.$formatters.push(validateEqual);<\/p>\n<p>scope.$watch(attrs.validateEquals, function() {<br \/>\nngModelCtrl.$setViewValue(ngModelCtrl.$viewValue);<br \/>\n});<br \/>\n}<br \/>\n};<br \/>\n}); From: http:\/\/sitestree.com\/?p=844<br \/> Categories:AngularJS, By Sayed Ahmed<br \/>Tags:<br \/> Post Data:2014-02-16 10:00:57<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Building Your Own Directives Directives can appear as HTML elements, attributes, comments, or CSS classes. Examples &lt;my-directive&gt;&lt;\/my-directive&gt; &lt;input my-directive&gt; &lt;!&#8211; directive: my-directive&#8211;&gt; &lt;input&gt; Defining a Directive angular.module(&#8216;app&#8217;, []).directive(&#8216;myDir&#8217;, function() { return myDirectiveDefinition; }); Writing a Button Directive escribe(&#8216;button directive&#8217;, function () { var $compile, $rootScope; beforeEach(module(&#8216;directives.button&#8217;)); beforeEach(inject(function(_$compile_, _$rootScope_) { $compile = _$compile_; $rootScope = _$rootScope_; &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=68351\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-68351","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":7438,"url":"http:\/\/bangla.sitestree.com\/?p=7438","url_meta":{"origin":68351,"position":0},"title":"AngularJS \u098f \u09a8\u09bf\u099c\u09c7\u09b0\/Custom Directives \u09a4\u09c8\u09b0\u09bf\u0964 building-your-own-directives-in-angularjs","author":"Author-Check- Article-or-Video","date":"March 23, 2015","format":false,"excerpt":"AngularJS \u098f\u09b0 \u09ae\u09be\u09a7\u09cd\u09af\u09ae\u09c7 \u09a8\u09bf\u099c\u09c7\u09b0 Directives \u09a4\u09c8\u09b0\u09bf\u0964 Md. Shahinoor Islam\u09a8\u09bf\u099c\u09c7\u09b0 Directives \u09a4\u09c8\u09b0\u09bf\u0964 HTML \u0989\u09aa\u09be\u09a6\u09be\u09a8, \u09ae\u09a8\u09cd\u09a4\u09ac\u09cd\u09af \u09ac\u09be CSS classes \u09a6\u09bf\u09df\u09c7 Directives \u09aa\u09cd\u09b0\u09a6\u09b0\u09cd\u09b6\u09a8 \u0995\u09b0\u09a4\u09c7 \u09aa\u09be\u09b0\u0964 \u0989\u09a6\u09be\u09b9\u09b0\u09a8\u0983 <my-directive><\/my-directive> <input my-directive> <!\u2013 directive: my-directive\u2013> <input> \u098f\u0995\u099f\u09bf Directive \u098f\u09b0 \u09a7\u09be\u09b0\u09a8\u09be angular.module(\u2018app\u2019, []).directive(\u2018myDir\u2019, function() { return myDirectiveDefinition; }); \u098f\u0995\u099f\u09bf Button Directive \u09b2\u09c7\u0996\u09be escribe(\u2018button directive\u2019, function () {\u2026","rel":"","context":"In &quot;AngularJS - 001&quot;","block_context":{"text":"AngularJS - 001","link":"http:\/\/bangla.sitestree.com\/?cat=154"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5794,"url":"http:\/\/bangla.sitestree.com\/?p=5794","url_meta":{"origin":68351,"position":1},"title":"AngularJS HTML DOM","author":"Author-Check- Article-or-Video","date":"January 28, 2015","format":false,"excerpt":"AngularJS HTML DOM parijat biswas student of CSE university of Asia Pacific AngularJs Directives \u0997\u09c1\u09b2\u09cb HTML DOM \u0989\u09aa\u09be\u09a6\u09be\u09a8\u0997\u09c1\u09b2\u0995\u09c7 \u0985\u09cd\u09af\u09be\u09aa\u09cd\u09b2\u09bf\u0995\u09c7\u09b6\u09a8 \u09a1\u09be\u099f\u09be\u09a4\u09c7 \u09af\u09c1\u0995\u09cd\u09a4 \u0995\u09b0\u09c7 \u0964 ng-disabled Directive ng-disabled directive Angularjs \u0985\u09cd\u09af\u09be\u09aa\u09cd\u09b2\u09bf\u0995\u09c7\u09b6\u09a8 \u09a1\u09be\u099f\u09be\u0995\u09c7 disabled html \u0989\u09aa\u09be\u09a6\u09be\u09a8\u0997\u09c1\u09b2\u09cb\u09b0 \u09b8\u09be\u09a5\u09c7 \u09af\u09c1\u0995\u09cd\u09a4 \u0995\u09b0\u09c7 \u0964 AngularJS \u0989\u09a6\u09be\u09b9\u09b0\u09a8\u0983 <div ng-app=\"\"> <p> mySwitch\">Click Me! <\/p> <p> <input type=\"checkbox\" ng-model=\"mySwitch\">Button <\/p>\u2026","rel":"","context":"In &quot;AngularJS - 001&quot;","block_context":{"text":"AngularJS - 001","link":"http:\/\/bangla.sitestree.com\/?cat=154"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5792,"url":"http:\/\/bangla.sitestree.com\/?p=5792","url_meta":{"origin":68351,"position":2},"title":"AngularJS \u09a1\u09bf\u09b0\u09c7\u0995\u09cd\u099f\u09bf\u09ad (AngularJS Directives in Bangla)","author":"Author-Check- Article-or-Video","date":"January 27, 2015","format":false,"excerpt":"\u09aa\u09b0\u09be\u099c\u09bf\u09a4 \u09ac\u09bf\u09b6\u09cd\u09ac\u09be\u09b8 \u00a0 AngularJs \u0986\u09aa\u09a8\u09be\u0995\u09c7 \u09a8\u09a4\u09c1\u09a8 \u098f\u099f\u09cd\u09b0\u09bf\u09ac\u09bf\u0989\u099f Directives \u098f\u09b0 \u09ae\u09be\u09a7\u09cd\u09af\u09ae\u09c7 html \u0995\u09c7 \u09ac\u09cd\u09af\u09be\u0996\u09cd\u09af\u09be \u0995\u09b0\u09a4\u09c7 \u09b8\u09be\u09b9\u09be\u09af\u09cd\u09af \u0995\u09b0\u09ac\u09c7 \u0964 AngularJS \u098f\u09b0 \u098f\u0995 \u09b8\u09c7\u099f \u09ac\u09bf\u09b2\u09cd\u099f \u0987\u09a8 Directives \u0986\u099b\u09c7, \u09af\u09be \u0986\u09aa\u09a8\u09be\u09b0 \u09aa\u09cd\u09b0\u09a4\u09cd\u09af\u09c7\u0995\u099f\u09bf \u0985\u09cd\u09af\u09be\u09aa\u09cd\u09b2\u09bf\u0995\u09c7\u09b6\u09a8\u0995\u09c7 \u0995\u09be\u09b0\u09cd\u09af\u0995\u09b0\u09bf \u0995\u09b0\u09c7 \u09a4\u09cb\u09b2\u09c7\u0964 AngularJs \u0986\u09aa\u09a8\u09be\u0995\u09c7 \u0986\u09aa\u09a8\u09be\u09b0 \u09a8\u09bf\u099c\u09b8\u09cd\u09ac Directive \u09b8\u0982\u0999\u09cd\u0997\u09be\u09df\u09bf\u09a4 \u0995\u09b0\u09be\u09b0 \u09b8\u09c1\u09af\u09cb\u0997 \u09a6\u09c7\u09df\u0964 \u00a0 AngularJS \u09a1\u09bf\u09b0\u09c7\u0995\u09cd\u099f\u09bf\u09ad (AngularJS Directives) AngularJs \u0986\u09aa\u09a8\u09be\u0995\u09c7 ng- prefix\u2026","rel":"","context":"In &quot;AngularJS - 001&quot;","block_context":{"text":"AngularJS - 001","link":"http:\/\/bangla.sitestree.com\/?cat=154"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5775,"url":"http:\/\/bangla.sitestree.com\/?p=5775","url_meta":{"origin":68351,"position":3},"title":"AngularJS \u09aa\u09b0\u09bf\u099a\u09bf\u09a4\u09bf (AngularJS Introduction in Bangla)","author":"Author-Check- Article-or-Video","date":"January 19, 2015","format":false,"excerpt":"\u09aa\u09be\u09b0\u09bf\u099c\u09be\u09a4 \u09ac\u09bf\u09b6\u09cd\u09ac\u09be\u09b8 AngularJS \u098f\u0995\u099f\u09bf JavaScript \u098f\u09b0 framework \u0964 \u098f\u099f\u09bf HTML \u09aa\u09c7\u099c\u09c7\u09b0 \u09b8\u09be\u09a5\u09c7 <script> tag \u098f\u09b0 \u09ae\u09be\u09a7\u09cd\u09af\u09ae\u09c7 \u09af\u09cb\u0997 \u0995\u09b0\u09be \u09b9\u09df\u0964 AngularJS \u098f\u0987\u099a\u099f\u09bf\u098f\u09ae\u098f\u09b2 \u098f\u099f\u09cd\u09b0\u09bf\u09ac\u09bf\u0989\u099f \u098f\u09b0 \u09a8\u09bf\u09b0\u09cd\u09a6\u09c7\u09b6\u09a8\u09be (Directive) \u0985\u09a8\u09c1\u09b8\u09be\u09b0\u09c7 \u09aa\u09cd\u09b0\u09b8\u09be\u09b0\u09bf\u09a4 \u0995\u09b0\u09c7 \u098f\u09ac\u0982 HTML \u098f\u09b0 Expression \u098f\u09b0 \u09b8\u09be\u09a5\u09c7 \u09a4\u09a5\u09cd\u09af \u09ae\u09bf\u09b6\u09cd\u09b0\u09bf\u09a4 \u0995\u09b0\u09c7 \u0964 \u00a0 AngularJS \u098f\u0995\u099f\u09bf JavaScript Framework AngularJS \u098f\u0995\u099f\u09bf JavaScript Framework \u0964 \u098f\u099f\u09be JavaScript\u2026","rel":"","context":"In &quot;AngularJS - 001&quot;","block_context":{"text":"AngularJS - 001","link":"http:\/\/bangla.sitestree.com\/?cat=154"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5788,"url":"http:\/\/bangla.sitestree.com\/?p=5788","url_meta":{"origin":68351,"position":4},"title":"\u0985\u09cd\u09af\u09be\u0999\u09cd\u0997\u09c1\u09b2\u09be\u09b0 \u099c\u09c7\u098f\u09b8 (AngularJS) \u09ab\u09b0\u09ae (Forms)","author":"Author-Check- Article-or-Video","date":"January 25, 2015","format":false,"excerpt":"\u0985\u09cd\u09af\u09be\u0999\u09cd\u0997\u09c1\u09b2\u09be\u09b0 \u099c\u09c7\u098f\u09b8 (AngularJS) \u09ab\u09b0\u09ae (Forms) Article By : Protap Chandra -------- \u0986\u099c\u0995\u09c7\u09b0 \u099f\u09bf\u0989\u099f\u09b0\u09bf\u09df\u09be\u09b2\u09c7 \u0986\u09ae\u09b0\u09be AngularJS \u09ab\u09b0\u09ae \u09b8\u09ae\u09cd\u09aa\u09b0\u09cd\u0995\u09c7 \u099c\u09be\u09a8\u09ac\u0964 AngularJS \u09ab\u09b0\u09ae \u09b9\u099a\u09cd\u099b\u09c7 HTML \u0987\u09a8\u09aa\u09c1\u099f element \u09b8\u09ae\u09c2\u09b9\u09c7\u09b0 \u09b8\u09c7\u099f, \u09af\u09be HTML controls \u09a8\u09be\u09ae\u09c7 \u09aa\u09b0\u09bf\u099a\u09bf\u09a4\u0964 \u09af\u09c7\u09ae\u09a8 - \u2022 input elements \u2022 select elements \u2022 button elements \u2022 textarea elements AngularJS \u09ab\u09b0\u09ae \u09ac\u09cd\u09af\u09ac\u09b9\u09be\u09b0\u09c7\u09b0 \u0989\u09a6\u09be\u09b9\u09b0\u09a3: formController\">\u2026","rel":"","context":"In &quot;AngularJS - 001&quot;","block_context":{"text":"AngularJS - 001","link":"http:\/\/bangla.sitestree.com\/?cat=154"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5804,"url":"http:\/\/bangla.sitestree.com\/?p=5804","url_meta":{"origin":68351,"position":5},"title":"AngularJS References","author":"Author-Check- Article-or-Video","date":"February 2, 2015","format":false,"excerpt":"AngularJS References parijat biswas student of CSE university of Asia Pacific AngularJS Directives \u09af\u09c7\u09b8\u09ac AngularJS Directives \u098f\u0987 \u099f\u09bf\u0989\u099f\u09cb\u09b0\u09bf\u09df\u09be\u09b2\u09c7 \u09ac\u09cd\u09af\u09ac\u09b9\u09c3\u09a4 \u09b9\u09df\u099b\u09c7\u0983 Directive \u09ac\u09b0\u09cd\u09a3\u09a8\u09be \u09ac\u09cd\u09af\u09be\u0996\u09cd\u09af\u09be ng-app \u098f\u0995\u099f\u09bf \u0985\u09cd\u09af\u09be\u09aa\u09cd\u09b2\u09bf\u0995\u09c7\u09b6\u09a8 \u098f\u09b0 \u09ae\u09c2\u09b2 \u0989\u09aa\u09be\u09a6\u09be\u09a8 \u09b8\u0982\u099c\u09cd\u099e\u09be\u09af\u09bc\u09bf\u09a4 \u0995\u09b0\u09be\u09b0 \u099c\u09a8\u09cd\u09af \u09ac\u09cd\u09af\u09ac\u09b9\u09c3\u09a4 Directives ng-bind HTML element \u098f\u09b0 innerHTML \u098f\u09aa\u09cd\u09b2\u09bf\u0995\u09c7\u09b6\u09a8 \u09a1\u09be\u099f\u09be\u09a4\u09c7 \u09af\u09c1\u0995\u09cd\u09a4 \u0995\u09b0\u09c7 \u0964 Introduction ng-click \u098f\u0995\u099f\u09bf \u0989\u09aa\u09be\u09a6\u09be\u09a8 \u0995\u09cd\u09b2\u09bf\u0995 \u0995\u09b0\u09be\u2026","rel":"","context":"In &quot;AngularJS - 001&quot;","block_context":{"text":"AngularJS - 001","link":"http:\/\/bangla.sitestree.com\/?cat=154"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/68351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=68351"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/68351\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=68351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=68351"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=68351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}