国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

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

2024-05-06 15:16:43
字體:
來源:轉載
供稿:網友

本文實例講述了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("錯誤");   }) };});            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 紫云| 金平| 洛扎县| 嘉义县| 青海省| 原阳县| 望都县| 广平县| 贺州市| 江津市| 集贤县| 澎湖县| 巴彦县| 靖州| 鹿泉市| 麻阳| 贺州市| 苏尼特右旗| 西丰县| 武山县| 贵南县| 东至县| 南川市| 江西省| 卢龙县| 鄂尔多斯市| 齐齐哈尔市| 鄯善县| 龙海市| 北流市| 岑巩县| 波密县| 搜索| 偏关县| 乾安县| 蒲城县| 师宗县| 枣阳市| 临西县| 东乡县| 宜君县|