Upgrade to Pro — share decks privately, control downloads, hide ads and more …

angular decorate

angular decorate

Use AngularJS' decorate function with ease

damienklinnert

April 09, 2014
Tweet

More Decks by damienklinnert

Other Decks in Programming

Transcript

  1. THE SIMPLE WAY angular.module(moduleName, []) .decorate(serviceName, function ($delegate) { //

    modify behavior here return $delegate; }); www.github.com/damienklinnert/angular-decorate
  2. // Decorator definition angular.module('$templateCache+get', ['ng']) .decorate('$templateCache', function ($delegate) { var

    _get = $delegate.get; $delegate.get = function (key) { return _get.call(_get, key + ".html"); }; return $delegate; }); // Use decorator in whole app angular.module('app', ['$templateCache+get', ...])
  3. // Decorator definition angular.module("$resource+baseUrl", ['ngResource']) .decorate('$resource', function ($delegate) { return

    function (baseUrl) { var ret = $delegate.apply(this, arguments); ret.baseUrl = baseUrl; return ret; }; }); // Use decorator in whole app angular.module('app', ['$resource+baseUrl', ...])
  4. What can you do with this? angular.module('$rootScope+instrument', []).decorate('$rootScope', function ($delegate)

    { $delegate.__proto__.$watch = function () { ... }; $delegate.__proto__.$apply = function () { ... }; return $delegate; });
  5. THANK YOU RELATED MATERIAL angular-decorate www.github.com/damienklinnert/angular-decorate Hacking Core Directives in

    AngularJS http://briantford.com/blog/angular-hacking-core.html Angular $provide http://docs.angularjs.org/api/auto/object/$provide