JS-Code:Wie teilt man die Daten in AngularJS auf?
$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-Code:
<h1 ng-repeat="x in records|filterOutput">{{x}}</h1>
Ich möchte Ausgabe:
"19%"
"C:13%"
"C:19.6%|D:23%"
Aber console.log
:
TypeError: input.indexOf is not a function
Was sollte Ich mache? Wie teilt man die Daten in AngularJS auf?
Ich frage mich, wie Sie Ihre Aussage $ scope.records = { "0.19", "C: 0.13 "," C: 0.196 | D: 0.23 "} ausgeführt? Außerdem funktioniert indexOf für Array und String. Führen Sie eine console.log unter $ scope.records = {"0.19", "C: 0.13", "C: 0.196 | D: 0.23"} oder {{records}} in html aus, um zu sehen, ob es in den Datensätzen –
Problem dort ist, dass Sie ein Objekt und kein Array haben ** indexOf ** funktioniert auf Arrays und Strings. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf – Azarus
$ scope.records selbst ist kein gültiger JSON, es kann nicht mit ng-repeat verwendet werden. ng-repeat benötigt ein Array, um kein Objekt zu wiederholen. – superUser