[英]Conditionally loading the template in a angular directive
我試圖有條件地加載指令模板,例如 -
.directive('truncate',["$window", function($window, $compile){
return {
restrict: 'A',
templateUrl: 'template/truncate.html',
link: function (scope, element, attrs) {
var height = element[0].offsetHeight;
var shouldBetruncated = height>200;
if(shouldBetruncated){
// want my template to load here, otherwise template is not loaded
};
}
}
}])
.run(function ($templateCache) {
$templateCache.put('template/truncate.html',
'template code'
);
})
反正有沒有實現這個?
/*This will solve you problem*/
link: ...
templateUrl: function() {
if(shouldBetruncated) {
return 'template/truncate.html';
} else {
return '';
}
}
有關詳細信息,您可以從html傳遞模板或根據條件生成
嘗試這個 ...
.directive('truncate',["$window", function($window, $compile){
return {
restrict: 'A',
templateUrl: 'template/truncate.html',
link: function (scope, element, attrs) {
var height = element[0].offsetHeight;
shouldBetruncated = height>200;
if(shouldBetruncated){
loadTemplate();
};
// function loadTemplate -- begins
var loadTemplate = function ($templateCache) {
$templateCache.put('template/truncate.html',
'template code'
);
};
// -- ends
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.