angularjsangular-templatecache

Where should I use $templateCache?


I want to use $templateCache service to add some html into the cache. In Angular documentation example, it only shows the case of using it in run:

var myApp = angular.module('myApp', []);
myApp.run(function($templateCache) {
  $templateCache.put('templateId.html', 'This is the content of the template');
});

However, when I am trying to add some templates through myApp config, there seems to be injector error. Is there a way to use templateCache not in RUN?

Thanks


Solution

  • run is the correct place to use the $templateCache. Since $templateCache is a service, it's not accessible in the configuration phase, because it hasn't been created yet. config is used to configure services (like $templateCache) using their providers. You can only inject providers to config.