最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How use ng-repeat for plain text AngularJS - Stack Overflow

matteradmin8PV0评论

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!

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!

Share Improve this question asked Jul 12, 2014 at 22:55 DeptrocoDeptroco 1291 gold badge3 silver badges11 bronze badges 5
  • I don't think you can pass multiple values at once in a hidden field : stackoverflow./questions/3073001/… – Christian Bonato Commented Jul 12, 2014 at 22:57
  • It's an unique value, spilted by as :) – Deptroco Commented Jul 12, 2014 at 22:58
  • If you need to know the "key" passed when you are fetching values through ng-repeat, keep the original statement, and try {{$index}}. – Christian Bonato Commented Jul 12, 2014 at 22:59
  • "unique value, spilted by as" -> Not very elegant... What are you trying to achieve? Pass x values according to user's selection? – Christian Bonato Commented Jul 12, 2014 at 23:01
  • for input just use ng-model , to append can use split() to create array which can run through ng-repeat – charlietfl Commented Jul 12, 2014 at 23:12
Add a ment  | 

1 Answer 1

Reset to default 6

I would either use a simple controller function:

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

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

Another option might be a filter:

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

//in your javascript
myApp.filter("mas", function () {
  return function masFilter(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.

Post a comment

comment list (0)

  1. No comments so far