如何分割字符串和使用`NG重复“ [英] How to split a string and use in `ng-repeat'
本文介绍了如何分割字符串和使用`NG重复“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从后端,我得到一个字符串作为施工,适用
- 这意味着2个字一起
From the back-end, I am getting a string as construction,apply
- that means 2 words together.
现在我需要将它们分解成2的话,我需要使用 NG-重复
。我创建了一个过滤器
来做到这一点。它分裂串入为2的长度,但如何在使用本 NG-重复
?
Now I need to break them into 2 words and I need to use the ng-repeat
. I created a filter
to do that. It splits the string into an array with a length of 2, but how to use this in ng-repeat
?
下面是我的code:
<div class="column design">
//app.Phase here is : `construction,apply`
<span ng-repeat="value in activeApp.Phase || commaBreak">{{$index}}</span> //instead of index i would like to show the word.
</div>
我的过滤器:
angular.module("tcpApp")
.filter("commaBreak",
function () {
return function (value) {
if (!value.length) return;
return value.split(',');
}
});
我在做什么错在这里?
What am I doing wrong here?
推荐答案
下面是:
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl', ['$scope', function($scope) {
$scope.name = "construction,apply";
}]);
myApp.filter("commaBreak",
function () {
return function ( value ) {
if( !value.length ) return;
return value.split(',');
}
});
在HTML的一部分,你需要使用单个或
On html part you need to use the single OR.
<div ng-controller="MyCtrl">
<p ng-repeat="value in name | commaBreak">{{value}}</p>
</div>
下面是工作的例子: http://jsfiddle.net/HB7LU/16459/
这篇关于如何分割字符串和使用`NG重复“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文