我想用更多的模板创建指令。所选模板取决于某些值(例如模板类型)。然后,如果我在html页面中调用了我的指令,并且更改了类型模板类型,则需要修改html模板。如下所示:
<template-factory template-type={{foo}}></template-factory>我想我可以创建一个包含所有模板的html模板,并从ng中选择-如果帮助。但我认为这不是很好。请帮我,为这个任务选择最好的解决方案。
发布于 2016-05-11 09:03:15
有趣的是,在指令中,您可以传递一个函数作为模板,然后该函数可以返回一个用于模板的字符串。
看一看Angularjs中的指令模板函数有什么好处?,看看它是如何完成的,以及利弊。
来自角文档
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';
}
};
});https://stackoverflow.com/questions/37157708
复制相似问题