Ng-repeat without an HTML element (this time really without any)

I want to get something like:

Line 1<br>
Line 2<br>
Line 3<br>
Line 4<br>
Line 5<br>

using ng-repeat. Lines must be separated by nothing but<br>

+5
source share
1 answer

Here is a simplified directive that hardcodes <br>

HTML:

<p hidden-repeat="lines"></p>

app.directive('hiddenRepeat',function($parse){
  return {
    link: function(scope, elem, attr){
      var data = $parse(attr.hiddenRepeat)(scope);
      if(data){
        for (var i=0;i< data.length;i++){ 
          elem.append(data[i]+ "<br />");
        }  
      }
    }
  };
});

See in action: http://plnkr.co/edit/Y8eahPYmBr5ohbWCInde?p=preview

This solution allows you to specify a directive in an attribute (in this case, hidden repetition). Using the version of the attribute of the directive, you can specify what a packaging element is (in this case, a paragraph).

+3
source

All Articles