溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用AngularJS怎么讀取JSON文件

發(fā)布時(shí)間:2021-04-09 17:54:43 來(lái)源:億速云 閱讀:194 作者:Leah 欄目:web開(kāi)發(fā)

使用AngularJS怎么讀取JSON文件?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

具體如下:

<!doctype html>
<meta charset="UTF-8">
<html ng-app='routingDemoApp'>
<head>
 <title>AJAX and promise</title>
 <link href="bootstrap.min.css" rel="external nofollow" rel="stylesheet">
 <link href="self.css" rel="external nofollow" rel="stylesheet">
</head>
<body >
<div class="panel panel-default" ng-controller="AjaxJson"> <!--創(chuàng)建控制器-->
 <div class="panel-body">
  <table class="table table-striped table-hover">
   <thead>
   <tr>
    <td>名</td>
    <td>種類(lèi)</td>
    <td>價(jià)格</td>
    <td>保質(zhì)期</td>
   </tr>
   </thead>
   <tbody>
   <tr ng-hide="products.length">
    <td colspan="4" class="text-center">沒(méi)有數(shù)據(jù)</td>
     <!--當(dāng)沒(méi)有數(shù)據(jù)的時(shí)候,顯示這行,有數(shù)據(jù)的時(shí)候,隱藏。-->
   </tr>
   <tr ng-repeat="item in products"> <!--將數(shù)據(jù)放到item里面,逐一讀取-->
    <td ng-bind="item.name"></td>
    <td ng-bind="item.category"></td>
    <td ng-bind="item.price"></td>
    <td ng-bind="item.expiry"></td>
   </tr>
   </tbody>
  </table>
  <p><button ng-click="LoadJson()">加載JSON數(shù)據(jù)</button></p><!--觸發(fā)函數(shù)-->
 </div>
</div>
<div class="panel panel-default" ng-controller="AjaxXml">
 <div class="panel-body">
  <table class="table table-striped table-hover">
   <thead>
   <tr>
    <td>名</td>
    <td>種類(lèi)</td>
    <td>價(jià)格</td>
    <td>保質(zhì)期</td>
   </tr>
   </thead>
   <tbody>
   <tr ng-hide="products.length">
    <td colspan="4" class="text-center">沒(méi)有數(shù)據(jù)</td>
   </tr>
   <tr ng-repeat="item in products">
    <td ng-bind="item.name"></td>
    <td ng-bind="item.category"></td>
    <td ng-bind="item.price"></td>
    <td ng-bind="item.expiry"></td>
   </tr>
   </tbody>
  </table>
  <p><button ng-click="LoadXml()">加載xml數(shù)據(jù)</button></p>
 </div>
</div>
<script src="angular.min.js"></script>
<script src="angular-ui-router.js"></script>
<script src="ajax2.js"></script>
</body>
</html>
/*js*/
var app=angular.module("routingDemoApp",[]);
app.controller("AjaxJson",function($scope,$http){
 $scope.LoadJson=function(){
  $http.get("json.json")
   .success(function(data){
    $scope.products = data;
   })
   .error(function(){
    alert("出錯(cuò)")
   });
 };
});
app.controller("AjaxXml",function($scope,$http){
 $scope.LoadXml = function(){
  $http.get("xml.xml")
   .success(function(data){
    $scope.products = [];
    var productsElements = angular.element(data.trim()).find("product");
    for(var i=0;i<productsElements.length;i++){
     var product = productsElements.eq(i);
     $scope.products.push({
      name:product.attr("name"),
      category:product.attr("category"),
      price:product.attr("price"),
      expiry:product.attr("expiry")
     });
    }
   })
   .error(function(){
    alert("錯(cuò)誤");
   })
 };
});
/*json*/
[
 {"name":"apple","category":"fruit","price":"1.5","expiry":10},
 {"name":"banana","category":"fruit","price":"1.3","expiry":14},
 {"name":"pears","category":"fruit","price":"1.2","expiry":15},
 {"name":"tuna","category":"fish","price":"1.0","expiry":16}
]
/*xml*/
<products>
 <product name="apple" category="fruit" price="1.5" expiry="10" />
 <product name="banana" category="fruit" price="14" expiry="14" />
 <product name="pears" category="fruit" price="1.3" expiry="13" />
 <product name="tuna" category="fish" price="1.2" expiry="12" />
</products>

JSON:

1)配置對(duì)應(yīng)的控制器,將scope和http服務(wù)注入該控制器中。

2)使用$http.get(),把將要讀取的數(shù)據(jù)文件的url寫(xiě)入。

3)使用回調(diào)函數(shù),成功時(shí),將所得的data賦給$scope作用域下的變量products。

4)由前臺(tái)使用no-repeat指令進(jìn)行遍歷逐一取出數(shù)據(jù)。

XML:

1)配置對(duì)應(yīng)的控制器,將$scope和http服務(wù)注入該控制器中。

2)使用$http.get(),把將要讀取的數(shù)據(jù)文件的url寫(xiě)入。

3)使用回調(diào)函數(shù),在success里面進(jìn)行成功讀取XML數(shù)據(jù)時(shí)的操作。

4)定義一個(gè)$scope創(chuàng)建的作用域下的(也就會(huì)前臺(tái)可以訪問(wèn))數(shù)組變量products,后面會(huì)將讀取到的數(shù)據(jù)逐一插入到里面。

5)定義一個(gè)數(shù)據(jù)變量productElements,將XML文件里面的<product> 里的信息賦值給他。這里使用了trim()方法,原因是使用JS讀取XML文件時(shí)前后會(huì)出現(xiàn)許多空字符。trim()方法可以將空字符去除。

6)使用for循環(huán),將變量productElements里面每個(gè)<product> 的內(nèi)容都插入到之前定義好的數(shù)組變量products里面。

7)由前臺(tái)使用no-repeat指令進(jìn)行遍歷逐一取出數(shù)據(jù)。

關(guān)于使用AngularJS怎么讀取JSON文件問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI