5

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 should be separated by nothing except <br>

Paul
  • 25,812
  • 38
  • 124
  • 247
  • 2
    I'd use a [directive](http://docs.angularjs.org/guide/directive) for this that takes an array of "lines" and displays them as such, e.g. ``. I have to run else I'd throw together an example for you. I'll check on this question later when I'm free. – Jay Mar 21 '13 at 16:19
  • Thank you. I always wonder at the ability of Angular to do a complex solution out of simplest things. – Paul Mar 21 '13 at 17:22

1 Answers1

3

Here is a simplistic directive that hard-codes the <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 it in action: http://plnkr.co/edit/Y8eahPYmBr5ohbWCInde?p=preview

This solution allows you to specify the directive in an attribute (In this case, hidden-repeat). By using the attribute version of a directive, you can specify what the wrapping element is (In this case, a paragraph).

checketts
  • 14,167
  • 10
  • 53
  • 82
  • +1 I like this version better - I'm not quite sure why I used an element directive for this task. I've removed my answer, and have edited yours so it no longer references my previous answer. – Jay Mar 22 '13 at 17:13
  • I actually did an element directive at first, but it was irking me that I couldn't completely remove/transclude it out of the way, that made me look into the attribute approach. – checketts Mar 22 '13 at 20:14