{"id":72375,"date":"2020-01-14T10:16:06","date_gmt":"2020-01-14T04:46:06","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=72375"},"modified":"2024-04-12T14:22:54","modified_gmt":"2024-04-12T08:52:54","slug":"decorators","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/decorators\/","title":{"rendered":"Decorators"},"content":{"rendered":"<p>Decorators are a design pattern that is used to separate modification or decoration of a class without modifying the original source code. In AngularJS, decorators are functions that allow a service, directive or filter to be modified prior to its usage.<\/p>\n<p>There are two ways to register decorators<\/p>\n<ul>\n<li>$provide.decorator, and<\/li>\n<li>decorator<\/li>\n<\/ul>\n<p>Each provide access to a $delegate, which is the instantiated service\/directive\/filter, prior to being passed to the service that required it.<\/p>\n<h4>$provide.decorator<\/h4>\n<p>The decorator function allows access to a $delegate of the service once it has been instantiated. For example:<\/p>\n<p>angular.module(&#8216;myApp&#8217;, [])<\/p>\n<p>.config([ &#8216;$provide&#8217;, function($provide) {<\/p>\n<p>$provide.decorator(&#8216;$log&#8217;, [<\/p>\n<p>&#8216;$delegate&#8217;,<\/p>\n<p>function $logDecorator($delegate) {<\/p>\n<p>var originalWarn = $delegate.warn;<\/p>\n<p>$delegate.warn = function decoratedWarn(msg) {<\/p>\n<p>msg = &#8216;Decorated Warn: &#8216; + msg;<\/p>\n<p>originalWarn.apply($delegate, arguments);<\/p>\n<p>};<\/p>\n<p>return $delegate;<\/p>\n<p>}<\/p>\n<p>]);<\/p>\n<p>}]);<\/p>\n<p>After the $log service has been instantiated the decorator is fired. The decorator function has a $delegate object injected to provide access to the service that matches the selector in the decorator. This $delegate will be the service you are decorating. The return value of the function provided to the decorator will take place of the service, directive, or filter being decorated.<\/p>\n<p>The $delegate may be either modified or completely replaced. Given a service myService with a method someFn, the following could all be viable solutions:<\/p>\n<p><strong>Completely Replace the $delegate<\/strong><\/p>\n<p>angular.module(&#8216;myApp&#8217;, [])<\/p>\n<p>.config([ &#8216;$provide&#8217;, function($provide) {<\/p>\n<p>$provide.decorator(&#8216;myService&#8217;, [<\/p>\n<p>&#8216;$delegate&#8217;,<\/p>\n<p>function myServiceDecorator($delegate) {<\/p>\n<p>var myDecoratedService = {<\/p>\n<p>\/\/ new service object to replace myService<\/p>\n<p>};<\/p>\n<p>return myDecoratedService;<\/p>\n<p>}<\/p>\n<p>]);<\/p>\n<p>}]);<\/p>\n<p><strong>Patch the $delegate<\/strong><\/p>\n<p>angular.module(&#8216;myApp&#8217;, [])<\/p>\n<p>.config([ &#8216;$provide&#8217;, function($provide) {<\/p>\n<p>$provide.decorator(&#8216;myService&#8217;, [<\/p>\n<p>&#8216;$delegate&#8217;,<\/p>\n<p>function myServiceDecorator($delegate) {<\/p>\n<p>var someFn = $delegate.someFn;<\/p>\n<p>function aNewFn() {<\/p>\n<p>\/\/ new service function<\/p>\n<p>someFn.apply($delegate, arguments);<\/p>\n<p>}<\/p>\n<p>$delegate.someFn = aNewFn;<\/p>\n<p>return $delegate;<\/p>\n<p>}<\/p>\n<p>]);<\/p>\n<p>}]);<\/p>\n<p><strong>Augment the $delegate<\/strong><\/p>\n<p>angular.module(&#8216;myApp&#8217;, [])<\/p>\n<p>.config([ &#8216;$provide&#8217;, function($provide) {<\/p>\n<p>$provide.decorator(&#8216;myService&#8217;, [<\/p>\n<p>&#8216;$delegate&#8217;,<\/p>\n<p>function myServiceDecorator($delegate) {<\/p>\n<p>function helperFn() {<\/p>\n<p>\/\/ an additional fn to add to the service<\/p>\n<p>}<\/p>\n<p>$delegate.aHelpfulAddition = helperFn;<\/p>\n<p>return $delegate;<\/p>\n<p>}<\/p>\n<p>]);<\/p>\n<p>}]);<\/p>\n<p>Note that whatever is returned by the decorator function will replace that which is being decorated. For example, a missing return statement will wipe out the entire object being decorated.<\/p>\n<p>Decorators have different rules for different services. This is because services are registered in different ways. Services are selected by name, however filters and directives are selected by appending &#8220;Filter&#8221; or &#8220;Directive&#8221; to the end of the name. The $delegate provided is dictated by the type of service.<\/p>\n<table>\n<thead>\n<tr>\n<td width=\"109\"><strong>Service Type <\/strong><\/td>\n<td width=\"197\"><strong>Selector <\/strong><\/td>\n<td width=\"313\"><strong>$delegate<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td width=\"109\">Service<\/td>\n<td width=\"197\">serviceName<\/td>\n<td width=\"313\">The object or function returned by the service<\/td>\n<\/tr>\n<tr>\n<td width=\"109\">Directive<\/td>\n<td width=\"197\">directiveName + &#8216;Directive&#8217;<\/td>\n<td width=\"313\">An Array.&lt;DirectiveObject&gt;*<\/td>\n<\/tr>\n<tr>\n<td width=\"109\">Filter<\/td>\n<td width=\"197\">filterName + &#8216;Filter&#8217;<\/td>\n<td width=\"313\">The function returned by the filter<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>* Multiple directives may be registered to the same selector\/name<\/p>\n<h4>module.decorator<\/h4>\n<p>This function is the same as the $provide.decorator function except it is exposed through the module API. This allows you to separate your decorator patterns from your module config blocks.<\/p>\n<p>Like with $provide.decorator, the module.decorator function runs during the config phase of the app. That means you can define a module.decorator before the decorated service is defined.<\/p>\n<p>Since you can apply multiple decorators, it is noteworthy that decorator application always follows order of declaration:<\/p>\n<ul>\n<li>If a service is decorated by both $provide.decorator and module.decorator, the decorators are applied in order:<\/li>\n<\/ul>\n<p>angular<\/p>\n<p>.module(&#8216;theApp&#8217;, [])<\/p>\n<p>.factory(&#8216;theFactory&#8217;, theFactoryFn)<\/p>\n<p>.config(function($provide) {<\/p>\n<p>$provide.decorator(&#8216;theFactory&#8217;, provideDecoratorFn); \/\/ runs first<\/p>\n<p>})<\/p>\n<p>.decorator(&#8216;theFactory&#8217;, moduleDecoratorFn); \/\/ runs seconds<\/p>\n<ul>\n<li>If the service has been declared multiple times, a decorator will decorate the service that has been declared last:<\/li>\n<\/ul>\n<p>angular<\/p>\n<p>.module(&#8216;theApp&#8217;, [])<\/p>\n<p>.factory(&#8216;theFactory&#8217;, theFactoryFn)<\/p>\n<p>.decorator(&#8216;theFactory&#8217;, moduleDecoratorFn)<\/p>\n<p>.factory(&#8216;theFactory&#8217;, theOtherFactoryFn);<\/p>\n<p>\/\/ `theOtherFactoryFn` is selected as &#8216;theFactory&#8217; provider and it is decorated via `moduleDecoratorFn`.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Decorators are a design pattern that is used to separate modification or decoration of a class without modifying the original source code. In AngularJS, decorators are functions that allow a service, directive or filter to be modified prior to its usage. There are two ways to register decorators $provide.decorator, and decorator Each provide access to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8417],"tags":[8442],"class_list":["post-72375","page","type-page","status-publish","hentry","category-angular-js-web-development","tag-decorators"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Decorators - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/decorators\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Decorators - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Decorators are a design pattern that is used to separate modification or decoration of a class without modifying the original source code. In AngularJS, decorators are functions that allow a service, directive or filter to be modified prior to its usage. There are two ways to register decorators $provide.decorator, and decorator Each provide access to...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/decorators\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:52:54+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/decorators\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/decorators\/\",\"name\":\"Decorators - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-14T04:46:06+00:00\",\"dateModified\":\"2024-04-12T08:52:54+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/decorators\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/decorators\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/decorators\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Decorators\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Decorators - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/decorators\/","og_locale":"en_US","og_type":"article","og_title":"Decorators - Tutorial","og_description":"Decorators are a design pattern that is used to separate modification or decoration of a class without modifying the original source code. In AngularJS, decorators are functions that allow a service, directive or filter to be modified prior to its usage. There are two ways to register decorators $provide.decorator, and decorator Each provide access to...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/decorators\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:52:54+00:00","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/decorators\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/decorators\/","name":"Decorators - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-14T04:46:06+00:00","dateModified":"2024-04-12T08:52:54+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/decorators\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/decorators\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/decorators\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Decorators"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/72375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=72375"}],"version-history":[{"count":3,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/72375\/revisions"}],"predecessor-version":[{"id":72549,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/72375\/revisions\/72549"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=72375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=72375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=72375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}