最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论