1. 程式人生 > >angularJS簡單掉用接口,實現數組頁面打印

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

link 接口 date 管理 span 你們 angular pan data

相比較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" 。

頁面展示如下:

技術分享

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