Javascript 如何在AngularJS中拆分数据?

Javascript 如何在AngularJS中拆分数据?,javascript,angularjs,Javascript,Angularjs,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('|')

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>
{{x}
我想要输出:

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

但是
console.log

TypeError:input.indexOf不是函数

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

我已经修改了您的代码,以获得所述的预期结果,请检查下面的代码。 为了更好地理解,我制作了一把小提琴

控制器:

.controller('FilterController', function ($scope) {
  $scope.records = ["0.19","C:0.13","C:0.196|D:0.23"];
});
过滤器:

.filter('filterOutput', function () {
        return function (inputArray) {
         return inputArray.map(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 + "%";
            }
         });
        }
    });

我已经修改了您的代码以获得所述的预期结果,请检查下面的代码。 为了更好地理解,我制作了一把小提琴

控制器:

.controller('FilterController', function ($scope) {
  $scope.records = ["0.19","C:0.13","C:0.196|D:0.23"];
});
过滤器:

.filter('filterOutput', function () {
        return function (inputArray) {
         return inputArray.map(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 track by $index">{{x|filterOutput}}</h1>
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 + "%";
        }
    }
})

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

Html代码

<h1 ng-repeat="x in records track by $index">{{x|filterOutput}}</h1>
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 + "%";
        }
    }
})

我想知道您的语句$scope.records={“0.19”、“C:0.13”、“C:0.196 | D:0.23}是如何执行的?此外,indexOf适用于数组和字符串。在html上的$scope.records={“0.19”、“C:0.13”、“C:0.196 | D:0.23}或{records}}下执行console.log,以查看它在记录中是否有任何内容问题在于您有一个对象,而不是数组indexOf对数组和字符串起作用$scope.records本身不是有效的json,不能与ng repeat一起使用。ng repeat需要数组来重复而不是对象。我想知道您的语句$scope.records={“0.19”、“C:0.13”、“C:0.196 | D:0.23}是如何执行的?此外,indexOf适用于数组和字符串。在html上的$scope.records={“0.19”、“C:0.13”、“C:0.196 | D:0.23}或{records}}下执行console.log,以查看它在记录中是否有任何内容问题在于您有一个对象,而不是数组indexOf对数组和字符串起作用$scope.records本身不是有效的json,不能与ng repeat一起使用。ng repeat需要一个数组来重复,而不是一个对象。谢谢。我在你的js'var\u input=input+''中这样做。它也可以解决我的问题。我可以问你另一个问题吗?我有一个数据
C:0.28
,然后输出是
28.0000000000000000004%
我想我没有处理一些细节。如果你想限制小数点,那么你可以使用.toFixed(小数点值)保存数值的变量。您可以在过滤器中使用下面提到的代码返回
returns[0]+':'+(数字(s[1])*100)。toFixed(2)+“%”谢谢。我在你的js'var\u input=input+''中这样做。它也可以解决我的问题。我可以问你另一个问题吗?我有一个数据
C:0.28
,然后输出是
28.0000000000000000004%
我想我没有处理一些细节。如果你想限制小数点,那么你可以使用.toFixed(小数点值)保存数值的变量。您可以在筛选器中使用下面提到的代码返回
returns[0]+':'+(数字(s[1])*100)。toFixed(2)+“%”