簡體   English   中英

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

您的代碼存在一些格式問題。 首先,您需要在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')

這些更改將解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM