5

$templateCache一个人可以在保持对原始提供者的引用的同时覆盖核心提供者吗?我想覆盖$templateCache不区分大小写。

IE 之类的

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

但不那么 hacky,更多 DI 风格?

4

2 回答 2

4

我会说用于decorator修改Provider将在配置阶段完成的实际代码,然后再开始行动。

我们之所以使用$templateCacheProvider,是因为Provider附加的前缀表明它provider(它可以Directive在您修改指令 DDO 的指令时)。您必须将此代码放在config应用程序的阶段内。

代码

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;
    }]);
}
于 2016-06-28T14:39:33.073 回答
0

试试下面的代码,对我来说很完美。

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;
        }]);
}]);
于 2019-07-30T11:11:38.263 回答