作者:北斗七星 | 来源:互联网 | 2023-10-10 12:30
Caniuse$templateCacheinui-routerstemplate?我可以在ui-router的模板中使用$templateCache吗?Thetemplate
Can i use $templateCache in ui-router's template?
我可以在ui-router的模板中使用$templateCache吗?
The template will be cached in resolve section and i want to use cached template in the same state.
该模板将缓存在resolve部分,我希望在相同的状态下使用缓存模板。
$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.
我认为我的代码的解析部分有一个问题。因为它运行在模板部分之后!它导致返回$templateCache。得到被定义。
I use ocLazyLoad plugin to cache template and it returns a correct promise.
我使用ocLazyLoad插件缓存模板,它返回一个正确的承诺。
Why template don't waits for resolve?
为什么模板不等待解决?
3 个解决方案
19
The way how to dynamically set the dynamic template is not via the template
property but templateProvider
. There is a working plunker, and this is the snippet:
如何动态地设置动态模板不是通过模板属性,而是通过templateProvider。有一个工作的活塞,这是它的代码片段:
// this is a run event (executed after config in fact)
// in which we do inejct a value into $templateCache
.run(function($templateCache){
// this could be lazy... elswhere
$templateCache.put('templates/template1.html'
, 'dashboard
');
})
.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/dashboard');
$stateProvider.state('dashboard', {
url: '/dashboard',
// this is the place where to resolve dynamic template
templateProvider: function($templateCache){
// simplified, expecting that the cache is filled
// there should be some checking... and async $http loading if not found
return $templateCache.get('templates/template1.html');
},
})
});
See:
看到的:
And also, I would say, that not ownly you can use the $templateCache
, but it is already used by ui-router
. The main service responsible for loading templates (from url, string...) for our views is the:
而且,我想说,你不能只使用$templateCache,它已经被ui-router使用了。负责为我们的视图加载模板(从url、string…)的主要服务是:
- $templateFactory
- templateFactory美元
which, as its code shows, does use $templateCache
as a natural optimization ($templateFactory
code snippet:)
正如其代码所示,它使用$templateCache作为自然优化($templateFactory代码片段:)
...
/**
* @ngdoc function
* @name ui.router.util.$templateFactory#fromUrl
* @methodOf ui.router.util.$templateFactory
*
* @description
* Loads a template from the a URL via `$http` and `$templateCache`.
*
* @param {string|Function} url url of the template to load, or a function
* that returns a url.
* @param {Object} params Parameters to pass to the url function.
* @return {string|Promise.} The template html as a string, or a promise
* for that string.
*/
this.fromUrl = function (url, params) {
if (isFunction(url)) url = url(params);
if (url == null) return null;
else return $http
.get(url, { cache: $templateCache })
.then(function(response) { return response.data; });
};
...