Can i use $templateCache in ui-router's template?
The template will be cached in resolve section and i want to use cached template in the same state.
$stateProvider
.state('dashboard', {
url: "/dashboard",
template: function($templateCache){
console.log('test 2');
return $templateCache.get('templates/template1.html'); // returns undefined
},
resolve:{
baseTemplates: function($ocLazyLoad) {
// here the template will be cached...
return $ocLazyLoad.loadTemplateFile(['base/dashboard.html']).then(function(){
console.log('test 1');
});
}
}
})
// console prints "test 2" before than "test 1"
Update: (+ Code updated)
I Think resolve section of my code has an issue. because it runs after template section! and it cause returning $templateCache.get being undefined.
I use ocLazyLoad plugin to cache template and it returns a correct promise.
Why template don't waits for resolve?
I do not know if it has already been solved, however, I decided to update my template as follows. I remember that I use AngularAMD / AngularUIRouter, but the templateURL works the same way. I use the function in templateURL, where I pass the "xxx" parameter equal to a new date every time, and this forces the page search again. I hope I have helped okay.
Once this is done, the code below works as expected:
Well, I just found out that you can simply set the key of your
$templateCache
template as thetemplateUrl
route property and it works fine.The way how to dynamically set the dynamic template is not via the
template
property buttemplateProvider
. There is a working plunker, and this is the snippet:See:
And also, I would say, that not ownly you can use the
$templateCache
, but it is already used byui-router
. The main service responsible for loading templates (from url, string...) for our views is the:which, as its code shows, does use
$templateCache
as a natural optimization ($templateFactory
code snippet:)