麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JavaScript > 正文

AngularJS讀取JSON及XML文件的方法示例

2019-11-19 16:29:19
字體:
來源:轉載
供稿:網友

本文實例講述了AngularJS讀取JSON及XML文件的方法。分享給大家供大家參考,具體如下:

<!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"> <!--創建控制器--> <div class="panel-body">  <table class="table table-striped table-hover">   <thead>   <tr>    <td>名</td>    <td>種類</td>    <td>價格</td>    <td>保質期</td>   </tr>   </thead>   <tbody>   <tr ng-hide="products.length">    <td colspan="4" class="text-center">沒有數據</td>     <!--當沒有數據的時候,顯示這行,有數據的時候,隱藏。-->   </tr>   <tr ng-repeat="item in products"> <!--將數據放到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數據</button></p><!--觸發函數--> </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>種類</td>    <td>價格</td>    <td>保質期</td>   </tr>   </thead>   <tbody>   <tr ng-hide="products.length">    <td colspan="4" class="text-center">沒有數據</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數據</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("出錯")   }); };});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("錯誤");   }) };});
/*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)配置對應的控制器,將scope和http服務注入該控制器中。

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

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

4)由前臺使用no-repeat指令進行遍歷逐一取出數據。

XML:

1)配置對應的控制器,將$scope和http服務注入該控制器中。

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

3)使用回調函數,在success里面進行成功讀取XML數據時的操作。

4)定義一個$scope創建的作用域下的(也就會前臺可以訪問)數組變量products,后面會將讀取到的數據逐一插入到里面。

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

6)使用for循環,將變量productElements里面每個<product> 的內容都插入到之前定義好的數組變量products里面。

7)由前臺使用no-repeat指令進行遍歷逐一取出數據。

PS:這里再為大家提供幾款關于xml與json操作的在線工具供大家參考使用:

在線XML/JSON互相轉換工具:
http://tools.VeVB.COm/code/xmljson

在線格式化XML/在線壓縮XML
http://tools.VeVB.COm/code/xmlformat

XML在線壓縮/格式化工具:
http://tools.VeVB.COm/code/xml_format_compress

在線JSON代碼檢驗、檢驗、美化、格式化工具:
http://tools.VeVB.COm/code/json

JSON在線格式化工具:
http://tools.VeVB.COm/code/jsonformat

在線json壓縮/轉義工具:
http://tools.VeVB.COm/code/json_yasuo_trans

更多關于AngularJS相關內容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結》、《AngularJS入門與進階教程》及《AngularJS MVC架構總結

希望本文所述對大家AngularJS程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲免费片 | 国产精品一区视频 | 午夜视频在线 | 国产精品久久久乱弄 | 欧美成人福利 | 国产免费资源 | 国产午夜三级一区二区三桃花影视 | 黄色免费电影网址 | 欧美日韩成人一区二区 | 九九热精品视频在线播放 | 日韩精品中文字幕一区 | 国产免费一区二区三区最新不卡 | 特级a欧美做爰片毛片 | 8x成人在线电影 | 免费在线观看国产 | 天天看天天摸天天操 | 在线中文字幕网站 | 国产一级毛片高清视频 | 日本在线一区二区 | 日本一区视频在线观看 | 成人福利视频在线观看 | 中文字幕一区在线观看视频 | 91精品一区二区综合在线 | 成人毛片100免费观看 | 亚洲码无人客一区二区三区 | sese综合| 嗯~啊~用力~高h | 日本网站一区 | 热re91久久精品国产99热 | 看黄在线观看 | 黄色免费小网站 | 亚洲射逼 | 99在线热视频 | 国产精品一区二区视频 | 欧美人成在线视频 | 国产精品一区二区三区在线播放 | www.成人在线 | 欧美一区二区三区久久精品视 | 九草在线| www.99久久久 | 成人mm视频在线观看 |