I couldn't find any documentation or article on this.
We here at the company I work for have MVC project.
And we build Angular SPA on top of it.
Also we want it to work offline.
So, I need cache. Found $templateCache module in AngularJS.
And trying to implement it. Since the project is MVC, all the templates I want to load into ng-view are actually MVC partial views and I can load them by calling {Controller}/{Action}.
But the are no examples on internet ho to implement $templateCache in this case.
All the examples show how to use static templates, like mytemplate.html or just strings. This won't work under MVC.
So, I was trying to figure out how to accomplish that, wrote this, for app.ts:

namespace AppDomain {
"use strict";

export let app = angular.module("app", ["ngRoute"]);

app.config(function ($routeProvider) {
    $routeProvider
        .when("/", { templateUrl: "Home/Template?name=Main", controller: "Controller", controllerAs: "vm" })
        .when("/About", { templateUrl: "Home/Template?name=About", controller: "Controller", controllerAs: "vm" })
        .when("/Contact", { templateUrl: "Home/Template?name=Contact", controller: "Controller", controllerAs: "vm" })
        .otherwise({ redirectTo: "/" });
});

app.run(function ($templateCache) {
    $templateCache.put("Home/Template?name=Main", "Home/Template?name=Main");
    $templateCache.put("Home/Template?name=About", "Home/Template?name=About");
    $templateCache.put("Home/Template?name=Contact", "Home/Template?name=Contact");
});

}

Obviously this doesnt work. Any suggestions? Thanks.

share|improve this question
    
From the documentation, how I understand it is that when you type $templateCache.put("Home/Template?name=Main", "Home/Template?name=Main"); the HTML being cached is the actual string "Home/Template?name=Main". I do not think it attemts to lookup by url. So my guess would be something like (mocked up, $http needs to handle callback): $templateCache.put("Home/Template?name=Main", $http.get("Home/Template?name=Main")); – cYrixmorten 18 hours ago
    
That being said, it looks odd to me that all your urls points to Template?name=something. Perhaps it is an approach that I am unaware of, but I would have a more straight forward .when("/", { templateUrl: "home" .. }) – cYrixmorten 18 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.