[英]Angular external templating vs Handlebars external templating
這就是我在Handlebarsjs中加載和編譯模板的方式,
define([
'jquery',
'handlebars'
],
function ($, Handlebars) {
// @reference: http://berzniz.com/post/24743062344/handling-handlebars-js-like-a-pro
Handlebars.getTemplate = function(name) {
if (Handlebars.templates === undefined || Handlebars.templates[name] === undefined) {
$.ajax({
url : 'js/template/' + name + '.handlebars',
success : function(data) {
if (Handlebars.templates === undefined) {
Handlebars.templates = {};
}
Handlebars.templates[name] = Handlebars.compile(data);
},
dataType: "text",
async : false
});
}
return Handlebars.templates[name];
};
// Precompile your templates and add/paste them below here.
});
我可以在下面的這一行中檢索模板,
this.$el.html(Handlebars.getTemplate('list')({contacts: response}));
因此,我不必反復使用ajax來調用同一模板。 我可以從車把的存儲中獲取它。
Angularjs有可能嗎? 我一直在尋找在Angualrjs中執行此操作的教程,但找不到任何內容。 有什么好的起點嗎?
Angular.js在請求模板時會緩存模板(因此請求僅發生一次),但是您可以使用$templateCache
預填充該緩存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.