逗号分隔< P>角 [英] Comma Separated <p> angular

查看:116
本文介绍了逗号分隔< P>角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pretty基本情况(有些新的角度)。我试图转换JSON:

  [
    {'名':'尼克},
    {'名':'大卫'},
    {'名':'约翰'},
]

要:

 < P>尼克,大卫,约翰< / P>

但我无法弄清楚如何生成一个P。你怎么叫NG重复内的< P>

 < p NG重复=,在菜单项项> {{item.name}}< / P>


解决方案

一件事,将是有益的是建立一个地图过滤器,像这样:

  myModule.filter('地图',函数(){
  返回功能(输入,的propName){
    返回input.map(函数(项目){
      返回项目[作为propName]
    });
  };
});

这是映射到指定属性的简单的例子,你可以使它更看中和理解点符号等。现在这种情况,在你看​​来,你可以这样做:

 < P>
  {{(菜单项|图:'名')加入('')}}
&所述; / P>

由于地图过滤器返回一个数组,它有一个内置的已经在Javascript中加入方法。

I have a pretty basic scenario (somewhat new to angular). I am trying to convert JSON:

[
    {'name': 'Nick'},
    {'name': 'David'},
    {'name': 'John'},
]

To:

<p>Nick,David,John</p>

But I can not figure out how to generate a single "p." How do you call ng-repeat inside of the <p>

<p ng-repeat="item in menuItems">{{item.name}}</p>

解决方案

One thing that would be helpful is creating a "map" filter, like so:

myModule.filter('map', function() {
  return function(input, propName) {
    return input.map(function(item) {
      return item[propName];
    });
  };
});

That's the simple case of mapping to a named property, you could make it more fancy and understand dot notation, etc. Now with this, in your view you can do:

<p>
  {{(menuItems | map:'name').join(',')}}
</p>

Because the map filter returns an Array, which has a built-in join method already in Javascript.

这篇关于逗号分隔&LT; P&GT;角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆