拆分NG重复项目? [英] Split for ng repeat item?

查看:116
本文介绍了拆分NG重复项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问卷这在我的控制器:

Simple question- if I have this in my controller:

$scope.somedata = 'Item 1, Item 2'; // set from something else

有没有办法分裂somedata我认为在ngRepeat前pression?是这样的:

is there a way to split somedata for my view in a ngRepeat expression? Something like:

<div ng-repeat="item in somedata | split(',')">{{item}}</div>

这不工作...

which doesnt work...

或做我必须做的。

$scope.somedata = 'Item 1, Item 2'; // set from something else
$scope.items = somedata.split(',');

然后

<div ng-repeat="item in items>{{item}}</div>

我觉得我没有真正理解什么,我可以在AngularJS一个前pression做的。

I think I'm not really understanding what I can do in an expression in AngularJS.

推荐答案

该字符串右侧的 | 解析为过滤器名称(通常它们是串格式过滤器,但棱角分明的团队也提供了过滤过滤器,它返回一个滤波阵列(有点误导,因为这两个共享相同的名字)。你可以创建自己的拆分过滤器来完成你想要什么:

The string the right of the | is resolved to the name of a filter (usually they are string formatting filters, but the angular team has also provided the filter filter, which returns a filtered array (a bit misleading because the two share the same name). You could create your own split filter to accomplish what you want:

angular.module('myFilters', []).
  filter('split', function() {
    return function(input, delimiter) {
      var delimiter = delimiter || ',';

      return input.split(delimiter);
    } 
  });

您可以使用过滤器,像这样:

You could use the filter like so:

<div ng-repeat="item in somedata | split">

或指定一个分隔符:

<div ng-repeat="item in somedata | split:,">

考虑以上伪code,因为我没有测试过。

Consider the above pseudo-code because I haven't tested it.

乐plunker:<一href=\"http://plnkr.co/edit/hk6F0Y6p5YAXv6fyXfSz?p=$p$pview\">http://plnkr.co/edit/hk6F0Y6p5YAXv6fyXfSz?p=$p$pview

这篇关于拆分NG重复项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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