I have a route setup like this:
var myApp = angular.module('myApp', []).
config(['$routeProvider', function ($routeProvider) {
$routeProvider.
when('/landing', {
templateUrl: '/landing-partial',
controller: landingController
}).
when('/:wkspId/query', {
templateUrl: '/query-partial',
controller: queryController
}).
otherwise({
redirectTo: '/landing'
});
}]);
I want to be able to make angularjs download both the partials in the beginning and not when requested.
Is it possible?
If you use rails, you can use the asset pipeline to compile and shove all your haml/erb templates into a template module which can be appended to your application.js file. Checkout http://minhajuddin.com/2013/04/28/angularjs-templates-and-rails-with-eager-loading
I just use
eco
to do the job for me.eco
is supported by Sprockets by default. It's a shorthand for Embedded Coffeescript which takes a eco file and compile into a Javascript template file, and the file will be treated like any other js files you have in your assets folder.All you need to do is to create a template with extension .jst.eco and write some html code in there, and rails will automatically compile and serve the file with the assets pipeline, and the way to access the template is really easy:
JST['path/to/file']({var: value});
wherepath/to/file
is based on the logical path, so if you have file in/assets/javascript/path/file.jst.eco
, you can access the template atJST['path/file']()
To make it work with angularjs, you can pass it into the template attribute instead of templateDir, and it will start working magically!
Yes, there are at least 2 solutions for this:
script
directive (http://docs.angularjs.org/api/ng.directive:script) to put your partials in the initially loaded HTML$templateCache
(http://docs.angularjs.org/api/ng.$templateCache) from JavaScript if needed (possibly based on result of$http
call)If you would like to use method (2) to fill in
$templateCache
you can do it like this:Of course the templates content could come from a
$http
call:Here is the plunker those techniques: http://plnkr.co/edit/J6Y2dc?p=preview
Another method is to use HTML5's Application Cache to download all files once and keep them in the browser's cache. The above link contains much more information. The following information is from the article:
Change your
<html>
tag to include amanifest
attribute:A manifest file must be served with the mime-type
text/cache-manifest
.A simple manifest looks something like this:
Once an application is offline it remains cached until one of the following happens:
If you wrap each template in a script tag, eg:
Concatenate all templates into 1 big file. If using Visual Studio 2013, download Web essentials - it adds a right click menu to create an HTML Bundle.
Add the code that this guy wrote to change the angular
$templatecache
service - its only a small piece of code and it works: Vojta Jina's GistIts the
$http.get
that should be changed to use your bundle file:Your routes
templateUrl
should look like this:If you use Grunt to build your project, there is a plugin that will automatically assemble your partials into an Angular module that primes $templateCache. You can concatenate this module with the rest of your code and load everything from one file on startup.
https://npmjs.org/package/grunt-html2js