angularJS簡單調用接口,實現數組頁面打印


相比較jquery ,angular對這種接口數據處理起來會方便的多。這里舉例調用 中國天氣網的api接口。

首先肯定要引入angular.js這個不多說

<link rel="stylesheet" href="css/bootstrap.css" type="text/css"></link>
<script type="text/javascript"  src="./js/angular.js"></script>

其次js代碼如下:

    var app = angular.module("myApp", []);
    app.controller("myCtrl", ['$scope','$http', function($scope,$http) {
        var url='http://wthrcdn.etouch.cn/weather_mini?city='+'北京';
          $http.get(url).then(function (response) {
              $scope.cityname=response.data.data.city
              $scope.myweather= response.data.data.forecast;      
            });

    }]);

用ng-app管理angularjs 作用范圍,控制器ng-controller這個去寫你的方法。這些都是必須的
div代碼:

<body  ng-app="myApp">
<div  ng-controller="myCtrl">  
<div>
        <p  style="font-size: 18px;text-align: center;font-weight: 600; color: rgb(200,10,10)">{{cityname}}</p>
         <table   class="table"  id="tale">
             <th>日期</th>
             <th>風力</th>
             <th>風向</th>
             <th>最高溫度</th>
             <th>最低溫度</th>
             <th>天氣狀況</th>
             <tr  ng-repeat="i  in  myweather ">
                 <td>{{i.date}}</td>
                 <td>{{i.fengli}}</td>
                 <td>{{i.fengxiang}}</td>
                 <td>{{i.high}}</td>
                 <td>{{i.low}}</td>
                 <td>{{i.type}}</td>
             </tr>
         </table>
</div>
</body>

這里非常方便的是這個 :ng-repeat,循環打印出你想打印的數據。當然你們以后肯定會遇到類似這種:

這是因為你的數組中存在相同數據,所以你只需要在 ng-repeat中加入"track by $index"就好了, 例如ng-repeat=" i in  你的數據   track by $index" 。

頁面展示如下:

 本文屬作者原創,如有轉載請標明文章出處:http://www.cnblogs.com/mobeisanghai/p/7459020.html
作者:漠北桑海


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM