我想创建包含更多模板的指令。 所选模板取决于某些值(如模板类型)。 然后,如果我在 html 页面中调用我的指令,并更改类型模板类型,则需要更改 html 模板。 像这样:
<template-factory template-type={{foo}}></template-factory>
我认为我可以创建一个包含我所有模板的 html 模板,并从 ng-if 帮助中选择。但是我认为它不是很好。 请帮助我,为此任务选择最佳解决方案。
最佳答案
有趣的是,您可以在指令中传递一个函数作为您的模板,然后该函数可以返回一个用于模板的字符串。
看看What are the benefits of a directive template function in Angularjs?了解这是如何完成的以及优缺点。
来自angular docs :
angular.module('docsTemplateUrlDirective', [])
.controller('Controller', ['$scope', function($scope) {
$scope.customer = {
name: 'Naomi',
address: '1600 Amphitheatre'
};
}])
.directive('myCustomer', function() {
return {
templateUrl: function(elem, attr){
return 'customer-'+attr.type+'.html';
}
};
});
关于javascript - AngularJs 创建模板指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37157708/