After the template is loaded, angularjs caches it to the $templateCache service by default. In real production, templates can be cached in advance to a JavaScript file that defines the template, so that you do not need to load the template through XHR.
$templateCache service allows $http services to cache XHR template requests so that they are only requested once. When a template is taken, its contents are stored in the $templateCache and the template path is used as the key. For example, when you get the following instance directive, it requests templateurl and puts the contents of the template in the $templateCache:
1 angular.module (' myApp ' ($timeout) { 3 return 4 restrict: ' A ' , 5 scope: {ngmodel: ' = ' ', 6 Templateurl: ' views/templates/notification.html '
$templateCache will keep the contents of this template in the $templateCache (' views/templates/notification.html '). If you have previously stored the content of the instruction file required for the test in the $templateCache, you can use $templateCache to prevent the request from being generated in the unit test of the instruction. You can use excellent karma-ng-html2js-preprocessor packages to convert templates into angular modules that you can use in your tests.
Using $templateCache
When deploying an application in production, we want the app to load as quickly as possible and respond as much as possible. Using XHR to load a template may cause the Web app to feel slow or sluggish. Instead of extracting the template through XHR, you can forge the template cache load by wrapping the template as a JavaScript file and then transferring it along with other parts of the application. For more information on how to effectively package a template, refer to the $templateCache tool: Grunt-angular-templates.
By default, angular cannot find a template from a local $tempalteCache, it extracts the template through XHR. When a XHR request is slow, or the template is large, it can have a significant negative impact on the user experience of the app.
You can avoid this delay by "forging" $templateCache is already populated so that angular does not have to load the template from the remote. This technique can be implemented manually in JavaScript, just like this:
1 angular.module (' myApp ', [])2 . Run (function($templateCache) { 3 $templateCache. Put (' home.html ', ' This is the home template '); 4 });
Now, when angular needs to extract a template named home.html, it will find it in the $TEMPLATECAHCE without extracting it from the server.
AngularJs $templateCache