简体   繁体   English

根据对象属性在ngRepeat中创建html元素

[英]Create html elements in ngRepeat depending on object properties

I have an array of objects that have two fileds for example : 我有一个具有两个字段的对象数组,例如:

var array = [{type:"range", group:"group1"}, {type:"boolean", group:"group1"}, 
 {type:"input", group:"group3"}... ]

Now in HTML by iterating through the array I want to create a div for every different GROUP and put the all elements with that group inside. 现在在HTML中,通过遍历数组,我想为每个不同的GROUP创建一个div并将该组中的所有元素放入其中。 But by putting I mean creating Inputs or Radiobuttons or Dropdowns depending on the TYPE of the element. 但是,我的意思是根据元素的类型创建Inputs,Radiobuttons或Dropdowns。

Now I have done this using AngularJS and mostly JS by appending objects to existing ones and so on. 现在,我通过将对象附加到现有对象等来使用AngularJS和大多数JS完成此操作。 But I want to reduce as much as possible the usage of js because I am having issue with calling a function after the HTML is loaded ( Call function after HTML is loaded AngularJS ). 但是我想尽可能减少js的使用,因为在HTML加载后调用函数HTML加载后调用函数AngularJS )存在问题。

So I will be thankfull if someone give me an advice how this should look like mostly in html. 因此,如果有人给我建议,这在大多数情况下应该像html一样,我将不胜感激。 I imagine something like this : 我想象这样的事情:

<div ng-repeat="object in array track by $index">
  //if object.group div exists add to existing one (check maybe with js function ?)
     // if object.type is ... add ... 
     // else if objec.type is ... add ...
     ...
  //else object.group not exists create div with id object.group for example
     // if object.type is ... add ...
     // else if object.type is ... add ...
     ...

This should work for your case: 这应该适合您的情况:

<div ng-repeat="(key, value) in array | groupBy: 'group'">
  Group: {{key}}
    <div ng-repeat="object in value">
      <div ng-switch on="object.type">
        <div ng-switch-when="range">Range block</div>
        <div ng-switch-when="boolean">Boolean block</div>
        <div ng-switch-when="input">Input block</div>
      </div>
    </div>
  <br>
</div>

First looping groups elements, second will work with an elements in a group 首先循环组元素,第二个将与组中的元素一起使用

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

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