gpt4 book ai didi

angularjs - 如何分割字符串并在`ng-repeat'中使用

转载 作者:行者123 更新时间:2023-12-03 08:12:52 25 4
gpt4 key购买 nike

从后端,我得到的字符串为construction,apply-意味着2个单词在一起。

现在,我需要将它们分为2个字,并且需要使用ng-repeat。我创建了一个filter来做到这一点。它将字符串拆分为长度为2的数组,但是如何在ng-repeat中使用它呢?

这是我的代码:

    <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(',');
}
});


我在这里做错了什么?

最佳答案

这里是:

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部分,您需要使用单个OR。

<div ng-controller="MyCtrl">
<p ng-repeat="value in name | commaBreak">{{value}}</p>
</div>


这是工作示例: http://jsfiddle.net/HB7LU/16459/

关于angularjs - 如何分割字符串并在`ng-repeat'中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32048201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com