我正在创建一个表单,从后端获取字段。映射后,我有这样的事情:Angular2:使用管道动态地呈现模板
genericFilters: {
iboId: {
'display': false,
'template': ''
},
iboCode: {
'display': true,
'template': 'text_input_iboCode',
/*'template': 'text/iboCode'*/
},
iboName: {
'display': true,
'template': 'text_input_iboName'
},
iboSurname: {
'display': true,
'template': 'text_input_iboSurname'
},
iboRank: {
'display': false,
'template': 'multiselect_iboRank',
/*'template': 'select/multi_iboRank',*/
},
iboEmail: {
'display': false,
'template': 'text_input_iboEmail'
},
iboNewsletter: {
'display': true,
'template': 'simple_select_iboNewsletter',
/*'template': 'select/simple_iboNewsletter',*/
},
};
我这背后的想法是在一个应用程序级别创建表单域的每个字段类型(checkbox
,multiselect
,text
,radio
等)。并使用上面映射的JSON
将某个字段类型应用于来自后端的每个接收字段。
在我的例子中,场iboId
应有的字段类型<text_input_iboCode>
。
所以,在我看来,我不希望有这样的事情:
<text_input_iboCode></text_input_iboCode>
<text_input_iboName></text_input_iboName>
<text_input_iboSurname></text_input_iboSurname>
我真的希望有创作形式更加抽象的,是这样的:
<div *ngFor="let field of genericFilters | dynamicTemplateProcessorPipe">
{{field.template}} <!--This should equal '<text_input_iboName>' for example-->
</div>
问题:
我要问月亮吗?这甚至有可能吗?有其他更好的方法来实现吗?我滥用@Pipe
功能吗?
我实际上使用@Pipe
进行翻译,格式化,在模板中循环使用objects
等。我猜我也可以使用它们来return
a <fieldTemplate>
。
我将开始研究,看看<ng-template #fieldTemplate>
的使用是否也是一种可行的选择,同时我希望有人能够通过@Pipe
了解该功能的可行性。