AngularJS讀取JSON及XML文件的方法示例
本文實(shí)例講述了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"> <!--創(chuàng)建控制器-->
<div class="panel-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<td>名</td>
<td>種類</td>
<td>價(jià)格</td>
<td>保質(zhì)期</td>
</tr>
</thead>
<tbody>
<tr ng-hide="products.length">
<td colspan="4" class="text-center">沒有數(shù)據(jù)</td>
<!--當(dāng)沒有數(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>種類</td>
<td>價(jià)格</td>
<td>保質(zhì)期</td>
</tr>
</thead>
<tbody>
<tr ng-hide="products.length">
<td colspan="4" class="text-center">沒有數(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寫入。
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寫入。
3)使用回調(diào)函數(shù),在success里面進(jìn)行成功讀取XML數(shù)據(jù)時(shí)的操作。
4)定義一個(gè)$scope創(chuàng)建的作用域下的(也就會(huì)前臺(tái)可以訪問)數(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ù)。
PS:這里再為大家提供幾款關(guān)于xml與json操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
在線JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
http://tools.jb51.net/code/json
JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat
在線json壓縮/轉(zhuǎn)義工具:
http://tools.jb51.net/code/json_yasuo_trans
更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結(jié)》、《AngularJS入門與進(jìn)階教程》及《AngularJS MVC架構(gòu)總結(jié)》
希望本文所述對(duì)大家AngularJS程序設(shè)計(jì)有所幫助。
相關(guān)文章
詳解JavaScript的AngularJS框架中的表達(dá)式與指令
這篇文章主要介紹了JavaScript的AngularJS框架中的表達(dá)式與指令,文中羅列了幾個(gè)常用的指令屬性加以說明,需要的朋友可以參考下2016-03-03
AngularJS1.X學(xué)習(xí)筆記2-數(shù)據(jù)綁定詳解
本篇文章主要介紹了AngularJS1.X學(xué)習(xí)筆記2-數(shù)據(jù)綁定詳解,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-04-04
詳解AngularJS中module模塊的導(dǎo)入導(dǎo)出
本文給大家介紹angularjs中module模塊的導(dǎo)入導(dǎo)出,涉及到angularjs module相關(guān)知識(shí),對(duì)angularjs module感興趣的朋友一起看看吧2015-12-12
解決Angular.Js與Django標(biāo)簽沖突的方案
AngularJS和django的模板都是用{{}}來引用變量的,這就導(dǎo)致了沖突,所以這篇文章主要就給大家介紹了如何解決Angular.Js與Django標(biāo)簽沖突的方案,有需要的朋友們可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。2016-12-12
angularjs實(shí)現(xiàn)文字上下無縫滾動(dòng)特效代碼
這篇文章主要介紹了angularjs實(shí)現(xiàn)文字上下無縫滾動(dòng)特效代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
AngularJS bootstrap啟動(dòng)詳解及實(shí)例代碼
這篇文章主要介紹了AngularJS bootstrap啟動(dòng)的知識(shí),這里整理了相關(guān)資料及簡單實(shí)例代碼,,需要的朋友可以參考下2016-09-09
詳解如何構(gòu)建Angular項(xiàng)目目錄結(jié)構(gòu)
本篇文章主要介紹了詳解如何構(gòu)建Angular項(xiàng)目目錄結(jié)構(gòu),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
AngularJS頁面訪問時(shí)出現(xiàn)頁面閃爍問題的解決
這篇文章主要介紹了AngularJS框架使用中出現(xiàn)頁面閃爍問題的解決方法,閃爍問題一般是初始化未加載完畢造成的,需要的朋友可以參考下2016-03-03

