简体   繁体   中英

Display data from json file in ionic project

I'm having a problem regarding getting the output of the ionic project where the data fetched by a json file. Even though i double checked for syntax errors i couldn't find the error. I tried to use ionic commands but the result is same

 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. hc 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> 

There are some formatting issues with your code. First of all, you need to use double-quotes in your JSON file like so:

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

You are also not accessing the correct object. The following line:

$scope.cars=data;

Should be changed to:

$scope.cars=data.data;

data contains the $http.get result which is the entire JSON object so you would actually want to iterate through data.data .

You also have a missing single quote here:

$http.get('js/cars.json)

Change that to:

$http.get('js/cars.json')

These changes should fix your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM