Nitesh Katare Nitesh Katare -5 years ago 375
AngularJS Question

$templateCache not working in angular-config in Angular.js

I am getting "angular.min.js:6Uncaught Error: [$injector:modulerr]" when trying to use $templateCache in my app.config block, if i remove the $templateCache parameter from app.config then i do not see any errors. Please let me know if i missed out something. Thanks in advance.

var app = angular.module("app", ['ui.router'], function () {
console.log('Application Initiated Successfully...');

.run(function ($templateCache, $http) {
console.log("app is running");
$http.get("Views/home2.html", { cache: true }).success(function (html) {
$templateCache.put("Test.html", html);

app.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', '$templateCache', function ($stateProvider, $urlRouterProvider, $locationProvider, $templateCache) {

.state("/", {
url: "/",
templateUrl: "Views/home.html"

app.controller("indexController", ["$rootScope", "$scope", function ($rootScope, $scope) {
$scope.message = "Hi lets get started...";
} ]);

Answer Source

You cannot access service at time of angular config. You can access your service inside your block. $templateCache is a service that you are trying to use in your config block.

However, You can access it inside your resolve block of a state.

        .state("/", {
            url: "/",
            templateUrl: "Views/home.html",
            resolve: {
              data: function ($templateCache,dbService) {
              return dbService.getData();

You can only access providers at time of config block of an angular.

Find below link to understand more about config block of angular.

Below is a description from its official site.

  • Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.
  • Run blocks - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download