2017-02-24 68 views
0

我有形式轉換一個JsonString到angular.js陣列

[{ 
    "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" 
}] 

的JsonString有人可以幫我轉換這個字符串成angular.js數組。我試圖做幾個小時,最後不得不放棄。在此先感謝

+4

的可能的複製[如何在Javascript解碼JSON?](http://stackoverflow.com/questions/13817445/how-to-decode-json-in-javascript) – n00dl3

+1

,你可以使用'JSON。解析'得到一個字符串數組。 –

+4

它已經是一個數組 – Satpal

回答

0

您必須在應用程序控制器內分配它,然後您可以像訪問您的視圖中的數組訪問它。

<!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>