MOCKSTACKS
EN
Questions And Answers

More Tutorials









Angular Decorators

Decorate service, factory


Below is example of service decorator, overriding null date returned by service.

angular.module('app', [])
 .config(function($provide) {
 $provide.decorator('myService', function($delegate) {
 $delegate.getDate = function() { // override with actual date object
 return new Date();
 };
 return $delegate;
 });
 })
 .service('myService', function() {
 this.getDate = function() {
 return null; // w/o decoration we'll be returning null
 };
 })
 .controller('myController', function(myService) {
 var vm = this;
 vm.date = myService.getDate();
 });
<body ng-controller="myController as vm">
 <div ng-bind="vm.date | date:'fullDate'"></div>
</body>

Decorate directive


Directives can be decorated just like services and we can modify or replace any of it's functionality. Note that directive itself is accessed at position 0 in $delegate array and name parameter in decorator must include Directive suffix (case sensitive).

So, if directive is called myDate, it can be accessed using myDateDirective using $delegate[0].

Below is simple example where directive shows current time. We'll decorate it to update current time in one second intervals. Without decoration it will always show same time.

<body>
 <my-date></my-date>
</body>
angular.module('app', [])
 .config(function($provide) {
 $provide.decorator('myDateDirective', function($delegate, $interval) {
 var directive = $delegate[0]; // access directive
 directive.compile = function() { // modify compile fn
 return function(scope) {
 directive.link.apply(this, arguments);
$interval(function() {
 scope.date = new Date(); // update date every second
 }, 1000);
 };
 };
 
 return $delegate;
 });
 })
 .directive('myDate', function() {
 return {
 restrict: 'E',
 template: '<span>Current time is {{ date | date:\'MM:ss\' }}</span>',
 link: function(scope) {
 scope.date = new Date(); // get current date
 }
 };
 });


Conclusion

In this page (written and validated by ) you learned about AngularJS Decorators . What's Next? If you are interested in completing AngularJS tutorial, your next topic will be learning about: AngularJS Print.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.