站长资源网络编程

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

整理:jimmy2026/8/1浏览2
简介本文实例讲述了AngularJS读取JSON及XML文件的方法。分享给大家供大家参考,具体如下:AJAX and promise</t</div> <div class="news_infos"><div id="MyContent"><p>本文实例讲述了AngularJS读取JSON及XML文件的方法。分享给大家供大家参考,具体如下:</p> <div class="htmlcode"> <pre class="brush:js;"> <!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="/UploadFiles/2021-04-02/angular.min.js"> <div class="htmlcode"> <pre class="brush:js;"> /*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("错误"); }) }; }); </pre> </div> <div class="htmlcode"> <pre class="brush:js;"> /*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} ] </pre> </div> <p> </p> <div class="htmlcode"> <pre class="brush:xml;"> /*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> </pre> </div> <p><strong>JSON:</strong></p> <p>1)配置对应的控制器,将scope和http服务注入该控制器中。</p> <p>2)使用$http.get(),把将要读取的数据文件的url写入。</p> <p>3)使用回调函数,成功时,将所得的data赋给$scope作用域下的变量products。</p> <p>4)由前台使用no-repeat指令进行遍历逐一取出数据。</p> <p><strong>XML:</strong></p> <p>1)配置对应的控制器,将$scope和http服务注入该控制器中。</p> <p>2)使用$http.get(),把将要读取的数据文件的url写入。</p> <p>3)使用回调函数,在success里面进行成功读取XML数据时的操作。</p> <p>4)定义一个$scope创建的作用域下的(也就会前台可以访问)数组变量products,后面会将读取到的数据逐一插入到里面。</p> <p>5)定义一个数据变量productElements,将XML文件里面的<product> 里的信息赋值给他。这里使用了trim()方法,原因是使用JS读取XML文件时前后会出现许多空字符。trim()方法可以将空字符去除。</p> <p>6)使用for循环,将变量productElements里面每个<product> 的内容都插入到之前定义好的数组变量products里面。</p> <p>7)由前台使用no-repeat指令进行遍历逐一取出数据。</p> <p><strong>PS:这里再为大家提供几款关于xml与json操作的在线工具供大家参考使用:</strong></p> <p><strong>在线XML/JSON互相转换工具:<br> </strong>http://tools.jb51.net/code/xmljson</p> <p><strong>在线格式化XML/在线压缩XML:<br> </strong>http://tools.jb51.net/code/xmlformat</p> <p><strong>XML</strong><strong>在线压缩/格式化工具:<br> </strong>http://tools.jb51.net/code/xml_format_compress</p> <p><strong>在线JSON代码检验、检验、美化、格式化工具:<br> </strong>http://tools.jb51.net/code/json</p> <p><strong>JSON</strong><strong>在线格式化工具:<br> </strong>http://tools.jb51.net/code/jsonformat</p> <p><strong>在线json压缩/转义工具:<br> </strong>http://tools.jb51.net/code/json_yasuo_trans</p> <p>更多关于AngularJS相关内容感兴趣的读者可查看本站专题:《AngularJS指令操作技巧总结》、《AngularJS入门与进阶教程》及《AngularJS MVC架构总结》</p> <p>希望本文所述对大家AngularJS程序设计有所帮助。</p></div> </div> </div> <div class="share"> </div> <div class="nextinfo"> <p>上一篇:<a href="/3g/1/98603.html" title="AngularJS基于factory创建自定义服务的方法详解">AngularJS基于factory创建自定义服务的方法详解</a></p> <p>下一篇:<a href="/3g/1/98605.html" title="详解nodejs微信jssdk后端接口">详解nodejs微信jssdk后端接口</a></p> </div> <div class="otherlink"> <h2>最新资源</h2> <ul> <li><a href="/3g/1/623703.html" title="群星《奔赴!万人现场 第2期》[FLAC/分轨]">群星《奔赴!万人现场 第2期》[FLAC/分轨]</a></li> <li><a href="/3g/1/623702.html" title="群星《奇妙浪一夏 (上海迪士尼度假区音乐">群星《奇妙浪一夏 (上海迪士尼度假区音乐</a></li> <li><a href="/3g/1/623701.html" title="群星《奇妙浪一夏 (上海迪士尼度假区音乐">群星《奇妙浪一夏 (上海迪士尼度假区音乐</a></li> <li><a href="/3g/1/623700.html" title="【古典音乐】詹姆斯·高威《季节》1993[WA">【古典音乐】詹姆斯·高威《季节》1993[WA</a></li> <li><a href="/3g/1/623699.html" title="贝拉芳蒂《卡里普索之王》SACD[WAV+CUE]">贝拉芳蒂《卡里普索之王》SACD[WAV+CUE]</a></li> <li><a href="/3g/1/623698.html" title="小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE">小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE</a></li> <li><a href="/3g/1/623697.html" title="群星《欢迎来到我身边 电影原声专辑》[32">群星《欢迎来到我身边 电影原声专辑》[32</a></li> <li><a href="/3g/1/623696.html" title="群星《欢迎来到我身边 电影原声专辑》[FL">群星《欢迎来到我身边 电影原声专辑》[FL</a></li> <li><a href="/3g/1/623695.html" title="雷婷《梦里蓝天HQⅡ》 2023头版限量编号低">雷婷《梦里蓝天HQⅡ》 2023头版限量编号低</a></li> <li><a href="/3g/1/623694.html" title="群星《2024好听新歌42》AI调整音效【WAV分">群星《2024好听新歌42》AI调整音效【WAV分</a></li> </ul> </div> </div> <div class="sidebar"> <div class="cloud"><h2 class="hometitle">一句话新闻</h2><a href="/3G/1/604673.html"><ul>微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键<br><br>几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。<br>在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。</ul></a></div> </div> <div class="sidebar"> 友情链接:<a href="http://www.imxmx.com/" title="杰晶网络" target="_blank">杰晶网络</a> <a href="/" title="DDR爱好者之家" target="_blank">DDR爱好者之家</a> <a href="http://www.nqxw.com/" title="南强小屋" target="_blank">南强小屋</a> <a href="http://www.paidiu.com/" title="黑松山资源网" target="_blank">黑松山资源网</a> <a href="http://www.dyhadc.com/" title="白云城资源网" target="_blank">白云城资源网</a> <a href="/sitemap1.xml">站点地图</a> <a href="/sitemap.xml">SiteMap</a> </div> </article> <footer> <p>Design by <a href="http://m.ddrfans.com">DDR爱好者之家</a> <a href="http://m.ddrfans.com">http://m.ddrfans.com</a></p> </footer> <script src="/images3g/nav.js"></script> <script type="text/javascript"> jQuery.noConflict(); jQuery(function() { var elm = jQuery('#left_flow2'); var startPos = jQuery(elm).offset().top; jQuery.event.add(window, "scroll", function() { var p = jQuery(window).scrollTop(); jQuery(elm).css('position', ((p) > startPos) ? 'fixed' : ''); jQuery(elm).css('top', ((p) > startPos) ? '0' : ''); }); }); </script> </body> </html>