我可以在ui-router模板中使用$ templateCache嗎?
該模板將被緩存在解析部分,我想在相同的狀態使用緩存模板。
$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"
更新:(+代碼更新)
我覺得我的代碼解決部分有問題。因爲它在模板部分之後運行!它會導致返回$ templateCache.get未定義。
我使用ocLazyLoad插件來緩存模板,它返回一個正確的承諾。
爲什麼模板不等待解決?
你得到什麼錯誤?只是一個猜測,但你可能不得不使用'$ templateCacheProvider',因爲你正在定義module.config() - [providers](https://docs.angularjs.org/guide/providers)中的路由 – Benmj
對不起。那是一個錯誤。錯誤來自另一節。但主要問題還在那裏。 –