我试图制作一个在ng-repeat中使用的自定义指令。我的想法是,我有一个我要展示的书籍清单,这些书籍有不同的类型。我想加载一个单独的模板,具体取决于书的类型(恐怖书horrorTemplate.html
,科幻书籍scifiTemplate.html
等)。我也将在稍后根据书的类型设置一些范围变量,但我想首先加载正确的模板。我目前拥有以下内容:
指令:
app.directive('group', function() {
return {
scope: false,
templateUrl: function(elem, attr) {
switch (attr.template) {
case 'horror':
return 'horrorTemplate.html';
break;
case 'scifi':
return 'scifiTemplate.html';
break;
default:
return 'defaultTemplate.html';
break;
}
}
}
});
模板:
<div ng-repeat="book in books">
<book template="{{book.type}}"></book>
</div>
目前,该指令始终加载defaultTemplate.html
。我已经经历了很多stackoverflow的答案,但它们似乎都没有解决我遇到的确切问题。