简体   繁体   English

AngularJS创建动态模板

[英]AngularJS create dynamic template

I´m currently trying to create some kind of dynamic template, which fetches data and meta information from two separate services. 我目前正在尝试创建某种动态模板,该模板将从两个单独的服务中获取数据和元信息。 The final result should be an table showing the data with the correct labels. 最终结果应该是一个显示带有正确标签的数据的表。 My plan was to create HTML -templates with different basic designs like a table with 3,4 or 5 columns. 我的计划是创建具有不同基本设计的HTML模板,例如具有3、4或5列的表格。 Then a matching controller should fetch the meta data from a service which should return an array with the id of an attribute and its name which should be displayed in the table. 然后,匹配的控制器应从服务中获取元数据,该服务应返回一个数组,该数组具有属性idname (应在表中显示)。 So far so good but now comes the tricky part: 到目前为止还不错,但现在出现了棘手的部分:
The second service fetches the matching data for the template and the id of the meta data and the id of the actual data matches, so that you can match them correctly. 第二个服务获取的模板和匹配数据id的元数据和id的实际数据匹配的,这样就可以正确地匹配。

Here is the Code on plunkr: Link 这是关于plunkr的代码: 链接

The Main problem is the following piece of code: 主要问题是以下代码:

<tr ng-repeat="person in persons">
   <td>{{person.name}}</td> 
   <td>{{person.age}}</td>
   <td>{{person.postal}}</td>
</tr>  

In between the <td> -tags the data doesn´t get fetched dynamic but static. <td>标记之间,获取的数据不是动态的而是静态的。 I don´t want to explicitly say how my attributes are called but use the fetched data from the meta service to know how my attributes are called. 我不想明确说明如何调用我的属性,而是使用从元服务中获取的数据来知道如何调用我的属性。 So in the end the template should get the names of the attributes as well as the matching data. 因此,最后,模板应获取属性的名称以及匹配的数据。 Do you guys have any good idea for me how to solve this? 你们对我有什么好办法怎么解决吗?

EDIT 编辑
To make things a little more clear: 为了使事情更加清楚:
At the moment the data between the <td> tags is fetched static because the term {{person.name}} stands there hard coded. 目前, <td>标记之间的数据是静态获取的,因为术语{{person.name}}处于硬编码状态。 Let´s assume that the structure of the data chenges and there will be a country -attribute instead of the postal one. 让我们假设数据的结构是固定的,并且会有一个country属性而不是postal属性。 The template which stays the same still tries to get data from person.postal and not from person.country . 保持不变的模板仍尝试从person.postal而不是person.country获取数据。 So undefined or even an error would be the consequence. 因此,可能会导致undefined甚至错误。
To prevent this the names of the attributes should get fetched from a meta service and build in the HTML page. 为了防止这种情况,应该从元服务中获取属性名称,并在HTML页面中进行构建。

The workflow should look somehow like this: 工作流程应如下所示:

  1. fetch the metadata --> get the names of the attributes 获取元数据->获取属性名称
  2. Set the names of the attributes inside the 'HTML' to call the correct attribute 在“ HTML”中设置属性名称以调用正确的属性
  3. fetch the actual data with the attributes defined before 使用之前定义的属性获取实际数据
  4. Create the table 创建表

http://jsfiddle.net/Lt024p9h/1/ http://jsfiddle.net/Lt024p9h/1/

<div ng-controller="MyCtrl">
  <table border="1">
    <tr ng-repeat="person in persons">
       <td ng-repeat="attr in query.attribs">
         {{person[attr.id]}}
       </td>
    </tr>  
  </table>
</div>

Right so what is going on here? 对,这是怎么回事?

So going by your fiddle, what we do if get the attributes you wish to display then using the object as an associative array . 因此,按您的摆弄,如果获得想要显示的属性然后将该对象用作关联数组 ,我们该怎么办。

This does require that the attr.id and the properties match up. 这确实要求attr.id和属性匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM