在 ionic 项目中显示来自 json 文件的数据
Display data from json file in ionic project
我在获取 ionic 项目的输出时遇到问题,其中数据由 json 文件获取。即使我仔细检查了语法错误,我也找不到错误。我尝试使用离子命令,但结果是一样的
Angular modules
angular.module('starter', ['ionic'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
.controller('ListController',['$scope','$http', function($scope,$http){
$http.get('js/cars.json).success(function(data){
$scope.cars=data;
});
}]);
//Json file
{
data: [{
manufacturer: 'Porsche',
model: '911',
price: 135000,
wiki: 'Dr.-Ing. h.c. F. Porsche AG, usually shortened to Porsche AG, is a German automobile manufacturer specializing in high-performance sports cars, SUVs and sedans',
img: '2004_Porsche_911_Carrera_type_997.jpg'
},{
manufacturer: 'Nissan',
model: 'GT-R',
price: 80000,
wiki:'Nissan Motor Company Ltd, usually shortened to Nissan, is a Japanese multinational automobile manufacturer headquartered in Nishi-ku, Yokohama, Japan',
img: '250px-Nissan_GT-R.jpg'
},{
manufacturer: 'BMW',
model: 'M3',
price: 60500,
wiki:'Bayerische Motoren Werke AG, usually known under its abbreviation BMW, is a German luxury vehicles, motorcycle, and engine manufacturing company founded in 1916',
img: '250px-BMW_M3_E92.jpg'
},{
manufacturer: 'Audi',
model: 'S5',
price: 53000,
wiki:'Audi AG is a German automobile manufacturer that designs, engineers, produces, markets and distributes luxury vehicles. Audi oversees worldwide operations from its headquarters in Ingolstadt, Bavaria, Germany',
img: '250px-Audi_S5.jpg'
},{
manufacturer: 'Ford',
model: 'TT',
price: 40000,
wiki:'Audi AG is a German automobile manufacturer that designs, engineers, produces, markets and distributes luxury vehicles. Audi oversees worldwide operations from its headquarters in Ingolstadt, Bavaria, Germany',
img: '250px-2007_Audi_TT_Coupe.jpg'
}]
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="cordova.js"></script>
<script src="js/app.js"></script>
</head>
<body ng-app="starter">
<ion-pane>
<ion-header-bar class="bar-dark">
<h1 class="title">Vehicle Search</h1>
</ion-header-bar>
<div clas="bar bar-subheader item-input-inset bar-light">
<label class="item-input-wrapper">
<i class="icon ion-search placeholder-icon"></i>
<input type="search" placeholder="Search">
</label>
</div>
<ion-content ng-controller="ListController" class="has-subheader">
<ion-list>
<ion-item ng-repeat='item in cars' class="item-thumbnail-left item-text-wrap">
<img src="img/{{item.manufacturer}}.jpg" alt="{{item.model}} Photo">
<h2><b>{{item.manufaturer}}</b></h2>
<h3>{{item.model}}</h3>
<h4>{{item.price}}</h4>
<p>{{item.wiki}}</p>
</ion-item>
</ion-list>
</ion-content>
</ion-pane>
</body>
</html>
您的代码存在一些格式问题。首先,您需要在 JSON 文件中使用双引号,如下所示:
{
"data": [{
"manufacturer": "Porsche",
"model": "911",
"price": "135000",
"wiki": "Dr.-Ing. h.c. F. Porsche AG, usually shortened to Porsche AG, is a German automobile manufacturer specializing in high-performance sports cars, SUVs and sedans",
"img": "2004_Porsche_911_Carrera_type_997.jpg"
},{
"model": "GT-R",
"manufacturer": "Nissan",
"price": "80000",
"wiki":"Nissan Motor Company Ltd, usually shortened to Nissan, is a Japanese multinational automobile manufacturer headquartered in Nishi-ku, Yokohama, Japan",
"img": "250px-Nissan_GT-R.jpg"
},{
"manufacturer": "BMW",
"model": "M3",
"price": "60500",
"wiki":"Bayerische Motoren Werke AG, usually known under its abbreviation BMW, is a German luxury vehicles, motorcycle, and engine manufacturing company founded in 1916",
"img": "250px-BMW_M3_E92.jpg"
},{
"manufacturer": "Audi",
"model": "S5",
"price": "53000",
"wiki":"Audi AG is a German automobile manufacturer that designs, engineers, produces, markets and distributes luxury vehicles. Audi oversees worldwide operations from its headquarters in Ingolstadt, Bavaria, Germany",
"img": "250px-Audi_S5.jpg"
},{
"manufacturer": "Ford",
"model": "TT",
"price": "40000",
"wiki":"Audi AG is a German automobile manufacturer that designs, engineers, produces, markets and distributes luxury vehicles. Audi oversees worldwide operations from its headquarters in Ingolstadt, Bavaria, Germany",
"img": "250px-2007_Audi_TT_Coupe.jpg"
}]
}
您也没有访问正确的对象。下面一行:
$scope.cars=data;
应改为:
$scope.cars=data.data;
data
包含 $http.get
结果,它是整个 JSON 对象,因此您实际上想要遍历 data.data
.
你这里也少了一个单引号:
$http.get('js/cars.json)
将其更改为:
$http.get('js/cars.json')
这些更改应该可以解决您的问题。
我在获取 ionic 项目的输出时遇到问题,其中数据由 json 文件获取。即使我仔细检查了语法错误,我也找不到错误。我尝试使用离子命令,但结果是一样的
Angular modules
angular.module('starter', ['ionic'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
.controller('ListController',['$scope','$http', function($scope,$http){
$http.get('js/cars.json).success(function(data){
$scope.cars=data;
});
}]);
//Json file
{
data: [{
manufacturer: 'Porsche',
model: '911',
price: 135000,
wiki: 'Dr.-Ing. h.c. F. Porsche AG, usually shortened to Porsche AG, is a German automobile manufacturer specializing in high-performance sports cars, SUVs and sedans',
img: '2004_Porsche_911_Carrera_type_997.jpg'
},{
manufacturer: 'Nissan',
model: 'GT-R',
price: 80000,
wiki:'Nissan Motor Company Ltd, usually shortened to Nissan, is a Japanese multinational automobile manufacturer headquartered in Nishi-ku, Yokohama, Japan',
img: '250px-Nissan_GT-R.jpg'
},{
manufacturer: 'BMW',
model: 'M3',
price: 60500,
wiki:'Bayerische Motoren Werke AG, usually known under its abbreviation BMW, is a German luxury vehicles, motorcycle, and engine manufacturing company founded in 1916',
img: '250px-BMW_M3_E92.jpg'
},{
manufacturer: 'Audi',
model: 'S5',
price: 53000,
wiki:'Audi AG is a German automobile manufacturer that designs, engineers, produces, markets and distributes luxury vehicles. Audi oversees worldwide operations from its headquarters in Ingolstadt, Bavaria, Germany',
img: '250px-Audi_S5.jpg'
},{
manufacturer: 'Ford',
model: 'TT',
price: 40000,
wiki:'Audi AG is a German automobile manufacturer that designs, engineers, produces, markets and distributes luxury vehicles. Audi oversees worldwide operations from its headquarters in Ingolstadt, Bavaria, Germany',
img: '250px-2007_Audi_TT_Coupe.jpg'
}]
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="cordova.js"></script>
<script src="js/app.js"></script>
</head>
<body ng-app="starter">
<ion-pane>
<ion-header-bar class="bar-dark">
<h1 class="title">Vehicle Search</h1>
</ion-header-bar>
<div clas="bar bar-subheader item-input-inset bar-light">
<label class="item-input-wrapper">
<i class="icon ion-search placeholder-icon"></i>
<input type="search" placeholder="Search">
</label>
</div>
<ion-content ng-controller="ListController" class="has-subheader">
<ion-list>
<ion-item ng-repeat='item in cars' class="item-thumbnail-left item-text-wrap">
<img src="img/{{item.manufacturer}}.jpg" alt="{{item.model}} Photo">
<h2><b>{{item.manufaturer}}</b></h2>
<h3>{{item.model}}</h3>
<h4>{{item.price}}</h4>
<p>{{item.wiki}}</p>
</ion-item>
</ion-list>
</ion-content>
</ion-pane>
</body>
</html>
您的代码存在一些格式问题。首先,您需要在 JSON 文件中使用双引号,如下所示:
{
"data": [{
"manufacturer": "Porsche",
"model": "911",
"price": "135000",
"wiki": "Dr.-Ing. h.c. F. Porsche AG, usually shortened to Porsche AG, is a German automobile manufacturer specializing in high-performance sports cars, SUVs and sedans",
"img": "2004_Porsche_911_Carrera_type_997.jpg"
},{
"model": "GT-R",
"manufacturer": "Nissan",
"price": "80000",
"wiki":"Nissan Motor Company Ltd, usually shortened to Nissan, is a Japanese multinational automobile manufacturer headquartered in Nishi-ku, Yokohama, Japan",
"img": "250px-Nissan_GT-R.jpg"
},{
"manufacturer": "BMW",
"model": "M3",
"price": "60500",
"wiki":"Bayerische Motoren Werke AG, usually known under its abbreviation BMW, is a German luxury vehicles, motorcycle, and engine manufacturing company founded in 1916",
"img": "250px-BMW_M3_E92.jpg"
},{
"manufacturer": "Audi",
"model": "S5",
"price": "53000",
"wiki":"Audi AG is a German automobile manufacturer that designs, engineers, produces, markets and distributes luxury vehicles. Audi oversees worldwide operations from its headquarters in Ingolstadt, Bavaria, Germany",
"img": "250px-Audi_S5.jpg"
},{
"manufacturer": "Ford",
"model": "TT",
"price": "40000",
"wiki":"Audi AG is a German automobile manufacturer that designs, engineers, produces, markets and distributes luxury vehicles. Audi oversees worldwide operations from its headquarters in Ingolstadt, Bavaria, Germany",
"img": "250px-2007_Audi_TT_Coupe.jpg"
}]
}
您也没有访问正确的对象。下面一行:
$scope.cars=data;
应改为:
$scope.cars=data.data;
data
包含 $http.get
结果,它是整个 JSON 对象,因此您实际上想要遍历 data.data
.
你这里也少了一个单引号:
$http.get('js/cars.json)
将其更改为:
$http.get('js/cars.json')
这些更改应该可以解决您的问题。