【发布时间】:2015-11-24 12:02:47
【问题描述】:
我从角度开始
我想写一个函数,为每个state带来模板
而且只是在需要的时候。作为“延迟加载”的原理
然后在第一次使用时将其保存在缓存中
我写了这段代码 但是我有错误,我不知道我错了什么, 有些东西我在角度上不明白,所以我理解我的代码中的问题
这是我所有的代码:
http://plnkr.co/edit/qzKJUwNImVX8EGb3ymnT?p=preview
var app = angular.module('myApp', ['ui.router']);
var getTemplate = function(name) {
templates = {pages: 'content of pagesTemplate.html. I very wish !!'};
if (name in templates) {
return templates[name];
}
$http.get(name + 'Template.html' ).then(
function(response){
templates[name] = response.data;
return templates[name];
}
);
};
app.config(function($stateProvider, $urlRouterProvider, $locationProvider) {
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
$stateProvider
.state('pages', {
url: '/:page',
templateUrl: getTemplate(), // Exist in line 18 ▲
// Here I want to build a function or calld a function
// To bring me the template of the page
// But I want it will be stored in the cache, if it comes first time
// And next time I will take it from the cache by the name of the 'state' ('pages').
controller: function($stateParams) {
alert($stateParams.page);
// And here I want to receive From the server The content of the page By $stateParams.page
}
})
});
<a ui-sref="pages({ page: 'home' })">Home</a>
<div ui-view=""></div>
【问题讨论】:
标签: angularjs angular-ui-router angular-ui state