简体   繁体   中英

Override $templateCache to be case insensitive

Can one override core provider like $templateCache while maintaining reference to the original provider ? I'd like to override $templateCache to be case insensitive.

IE something like

var normalGet = $templateCache.get;
var normalPut = $templateCache.put;
$templateCache.get = function(key) { normalGet(key.toLowerCase()); };
$templateCache.put = function(key,value) { normalPut(key.toLowerCase(), value); };

But less hacky, more DI-style ?

I'd say use decorator to modify the actual Provider code which will be done at configuration phase before coming into action.

We used $templateCacheProvider because Provider appended prefix indicate that its provider (it can be Directive when you are modifying directive DDO of directive). You have to place this code inside config phase of your application.

Code

app.config(['$provide', Decorate]);
function Decorate($provide) {
  $provide.decorator('$templateCacheProvider', 
    ['$delegate', function($delegate) {
      var templateCache = $delegate[0];

      var normalGet = templateCache.get;
      var normalPut = templateCache.put;
      templateCache.get = function(key) { return normalGet(key.toLowerCase()); };
      templateCache.put = function(key,value) { normalPut(key.toLowerCase(), value); };

      return $delegate;
    }]);
}

Try below code, worked perfectly for me.

angular.module('utils').config(['$provide', ($provide) => {
    $provide.decorator('$templateCache',
        ['$delegate', ($delegate: ITemplateCacheService) => {
            let templateCache = $delegate;
            let caseSenstiveGet = templateCache.get;
            let caseSenstivePut = templateCache.put;
            templateCache.get = (key) => { return caseSenstiveGet(key.toLowerCase()); };
            templateCache.put = (key, value) => { return caseSenstivePut(key.toLowerCase(), value); };
            return $delegate;
        }]);
}]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM