2

I know that something like that, will work:

<span ng-repeat="item in items">{{item.name}}</span>

How can I do the same with plain text?

I would like to append {{items[0].name}}, {{items[1].name}}, {{items[2].name}} to my html in plain text :)

In my case, inside an input: <input type="hidden" name="items" value="{{items[0].name}},{{items[1].name}},{{items[2].name}}"/>

Thanks guys!

Deptroco
  • 129
  • 1
  • 3
  • 11

1 Answers1

6

I would either use a simple controller function:

<input name="items" value="{{commas(items)}}">

//in your controller.js
$scope.commas = function commas(items) {
    return items.join(",");
}

Another option might be a filter:

<input name="items" value="{{ items | commas }}">

//in your javascript
myApp.filter("commas", function () {
  return function commasFilter(list) {
    return list.join(",");
  }
});

But in general keep the logic in your templates straightforward and move to controllers, filters, or directives when the built-in ones are not sufficient.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • Both solutions are cool. But should I take it that there is no other way such as using the `ng-repeat` directive without HTML tags ? – Muhammad Gelbana Jun 08 '15 at 18:25