简体   繁体   English

Angular 1-复杂的ng-repeat对象/数组

[英]Angular 1 - complex ng-repeat objects/arrays

I have an array with 3 object then 1 object (or more depending on the choice of 3 first objects) then 5 arrays (or more according to the choice of the 5 arrays) 我有一个数组,其中包含3个对象,然后是1个对象(或更多,取决于3个第一个对象的选择),然后是5个数组(或更多,取决于5个数组的选择)

repeat="item in $ctrl.project[$ctrl.checkProject()].productType[$ctrl.ckeckProductType()]

//method to search for the item
      checkProject() {
        if (this.project.label === 'business') {
          return 0
        } else if(this.project.label === 'realEstate') {
          return 1
        } else if (this.project.label === 'smallBusiness'){
          return 2
        }
      }

And the same for the ckeckProductType() method ckeckProductType()方法相同

Is there not a more sophisticated solution to energize all this? 没有更复杂的解决方案来激发所有这些能量吗?

The simplest way is to wrap your result sequence into the method. 最简单的方法是将结果序列包装到方法中。 Smth like this: 像这样:

data-ng-repeat = "object in getMyObjects() track by $index"

With this you will move you selecting logic to the controller where it should be. 通过此操作,您可以将选择的逻辑移动到应该放置的控制器上。 But, it is not nice from the performance side. 但是,从性能方面来说,这不是很好。 Function will be called each digest and will eat your resources. 函数将被称为每个摘要,并且会消耗您的资源。

Another approach is to define sequence's dependencies (this.project.label and so on) and track their update. 另一种方法是定义序列的依赖项(this.project.label等)并跟踪其更新。 When they will be updated, you will change your sequence. 当它们将被更新时,您将更改顺序。 This approach requires more code and it little bit harder to maintain but it little bit faster (until your sequence not contain 1000+ items) 这种方法需要更多的代码,并且难以维护,但速度更快(直到您的序列不包含1000多个项目)

So it is up to you what way to go. 因此,由您自己决定要怎么走。 Hope this helps. 希望这可以帮助。

PS There is third approach. PS有第三种方法。 Merge all into one sequence and write filter for this. 将所有内容合并为一个序列,并为此编写过滤器。 PPS The last is possible but do not do this, it was a small evening joke. PPS最后可能的,但不要这样做,那是一个小小的晚上的玩笑。

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

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