温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用AngularJS怎么读取JSON文件

发布时间:2021-04-09 17:54:43 来源:亿速云 阅读:253 作者:Leah 栏目:web开发

使用AngularJS怎么读取JSON文件?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

具体如下:

<!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指令进行遍历逐一取出数据。

关于使用AngularJS怎么读取JSON文件问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI