我有一个
形式的JsonString[{
"mileage": 12033,
"name": "Ford",
"model": "Focus",
"engine": "3 gophers on a treadmill",
"color": "green"
}, {
"mileage": 85000,
"name": "Chevy",
"model": "mailbu",
"engine": "6 cylinder",
"color": "Maroon"
}, {
"mileage": 612033,
"name": "Ford",
"model": "F150",
"engine": "6 cylinder",
"color": "Green"
}, {
"mileage": 89500,
"name": "Pontiac",
"model": "G6",
"engine": "2 cylinder",
"color": "Blue"
}, {
"mileage": 17200,
"name": "Pontiac",
"model": "G8",
"engine": "3 cylinder",
"color": "Silver"
}, {
"mileage": 308,
"name": "Toyota",
"model": "Forerunner",
"engine": "6 cylinders",
"color": "Grey"
}, {
"mileage": 65328,
"name": "Volvo",
"model": "wagon",
"engine": "8 cyclinders while not in test mode otherwise 2 bicycle pedals",
"color": "Red"
}, {
"mileage": 50,
"name": "IKea",
"model": "Ronde",
"engine": "3 self propelled coasters",
"color": "wood panelling"
}, {
"mileage": 17200,
"name": "Pontiac",
"model": "Grand Am",
"engine": "8 cylinder",
"color": "rusty"
}]
有人可以帮我把这个字符串转换成angular.js中的数组。我试着做了几个小时,最后不得不放弃。提前致谢
答案 0 :(得分:0)
您必须在app控制器中分配它,然后您可以像在视图中的数组一样访问它。
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<p>Today's welcome message is:</p>
<p ng-repeat="x in car">{{x.name}}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$scope.car = [{
"mileage": 12033,
"name": "Ford",
"model": "Focus",
"engine": "3 gophers on a treadmill",
"color": "green"
}, {
"mileage": 85000,
"name": "Chevy",
"model": "mailbu",
"engine": "6 cylinder",
"color": "Maroon"
}, {
"mileage": 612033,
"name": "Ford",
"model": "F150",
"engine": "6 cylinder",
"color": "Green"
}, {
"mileage": 89500,
"name": "Pontiac",
"model": "G6",
"engine": "2 cylinder",
"color": "Blue"
}, {
"mileage": 17200,
"name": "Pontiac",
"model": "G8",
"engine": "3 cylinder",
"color": "Silver"
}, {
"mileage": 308,
"name": "Toyota",
"model": "Forerunner",
"engine": "6 cylinders",
"color": "Grey"
}, {
"mileage": 65328,
"name": "Volvo",
"model": "wagon",
"engine": "8 cyclinders while not in test mode otherwise 2 bicycle pedals",
"color": "Red"
}, {
"mileage": 50,
"name": "IKea",
"model": "Ronde",
"engine": "3 self propelled coasters",
"color": "wood panelling"
}, {
"mileage": 17200,
"name": "Pontiac",
"model": "Grand Am",
"engine": "8 cylinder",
"color": "rusty"
}];
});
</script>
</body>
</html>