How to use ng-repeat without an html element

后端 未结 8 1497
暖寄归人
暖寄归人 2020-11-30 23:35

I need to use ng-repeat (in AngularJS) to list all of the elements in an array.

The complication is that each element of the array will transform to ei

相关标签:
8条回答
  • 2020-12-01 00:38

    Update: If you are using Angular 1.2+, use ng-repeat-start. See @jmagnusson's answer.

    Otherwise, how about putting the ng-repeat on tbody? (AFAIK, it is okay to have multiple <tbody>s in a single table.)

    <tbody ng-repeat="row in array">
      <tr ng-repeat="item in row">
         <td>{{item}}</td>
      </tr>
    </tbody>
    
    0 讨论(0)
  • 2020-12-01 00:40

    I would like to just comment, but my reputation is still lacking. So i'm adding another solution which solves the problem as well. I would really like to refute the statement made by @bmoeskau that solving this problem requires a 'hacky at best' solution, and since this came up recently in a discussion even though this post is 2 years old, i'd like to add my own two cents:

    As @btford has pointed out, you seem to be trying to turn a recursive structure into a list, so you should flatten that structure into a list first. His solution does that, but there is an opinion that calling the function inside the template is inelegant. if that is true (honestly, i dont know) wouldnt that just require executing the function in the controller rather than the directive?

    either way, your html requires a list, so the scope that renders it should have that list to work with. you simply have to flatten the structure inside your controller. once you have a $scope.rows array, you can generate the table with a single, simple ng-repeat. No hacking, no inelegance, simply the way it was designed to work.

    Angulars directives aren't lacking functionality. They simply force you to write valid html. A colleague of mine had a similar issue, citing @bmoeskau in support of criticism over angulars templating/rendering features. When looking at the exact problem, it turned out he simply wanted to generate an open-tag, then a close tag somewhere else, etc.. just like in the good old days when we would concat our html from strings.. right? no.

    as for flattening the structure into a list, here's another solution:

    // assume the following structure
    var structure = [
        {
            name: 'item1', subitems: [
                {
                    name: 'item2', subitems: [
                    ],
                }
            ],
        }
    ];
    var flattened = structure.reduce((function(prop,resultprop){
        var f = function(p,c,i,a){
            p.push(c[resultprop]);
            if (c[prop] && c[prop].length > 0 )
              p = c[prop].reduce(f,p);
            return p;
        }
        return f;
    })('subitems','name'),[]);
    
    // flattened now is a list: ['item1', 'item2']
    

    this will work for any tree-like structure that has sub items. If you want the whole item instead of a property, you can shorten the flattening function even more.

    hope that helps.

    0 讨论(0)
提交回复
热议问题