JSコード:AngularJSでデータを分割する方法は?
$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 is not a function
どうすべき私がやります? AngularJSでデータを分割する方法は?
フィルタどのようにあなたの声明$ scope.records = { "0.19"、 "C: 0.13 "、" C:0.196 | D:0.23 "}が実行されたか?さらに、indexOfは配列と文字列に対して機能します。 $ scope.records = {"0.19"、 "C:0.13"、 "C:0.196 | D:0.23"}またはhtmlの{{records}}の下にconsole.logを記録してレコードに何かがあるかどうか確認してください –
問題はあなたが配列ではなくオブジェクトを持っていることです** indexOf **は配列と文字列で動作します。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf – Azarus
$ scope.records自体は有効なjsonではありません。ng-repeatでは使用できません。 ng-repeatには、オブジェクトを繰り返さないための配列が必要です。 – superUser