如何在AngularJS中拆分数据?

和PSweet

JS代码:

$scope.records={"0.19", "C:0.13", "C:0.196|D:0.23"} 
    .filter('filterOutput', function () {
        return function (input) {
            if (input.indexOf(":") != -1) {
                var out = input
                    .split('|')
                    .map(function (v) {
                        var s = v.split(':')
                        return s[0] + ':' + (Number(s[1]) * 100) + "%"
                    })
                    .join()
                return out;
            } else {
                return input * 100 + "%";
            }
        }
    })

HTML代码:

<h1 ng-repeat="x in records|filterOutput">{{x}}</h1>

我想要输出:

“ 19%” “ C:13%” “ C:19.6%| D:23%”

但是console.log

TypeError:input.indexOf不是函数

我应该怎么办?如何在AngularJS中分割数据?

马诺伊·古普塔(Manoj Gupta)

我已经修改了您的代码以获得所需的输出。

HTML代码

<h1 ng-repeat="x in records track by $index">{{x|filterOutput}}</h1>

可变值

$scope.records=["0.19","C:0.13","C:0.196|D:0.23"];

AngularJS过滤器

.filter('filterOutput', function () {
    return function (input) {
        if (input.indexOf(":") != -1) {
            var out = input
                .split('|')
                .map(function (v) {
                    var s = v.split(':')
                    return s[0] + ':' + (Number(s[1]) * 100) + "%"
                })
                .join('|')
            return out;
        } else {
            return input * 100 + "%";
        }
    }
})

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章