2015/10/02

AngularJS get json

JSON可以透過$http來取得


<!DOCTYPE html>
<html ng-app="myApp">
  <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
    <script type="text/javascript">
      var myApp = angular.module('myApp', []);
      myApp.controller('myController', function($scope, $http){
        $scope.array = ["車牌號碼", "起始站", "起始站:終點站"];

        $http.get('http://data.kaohsiung.gov.tw/Opendata/DownLoad.aspx?Type=2&CaseNo1=AP&CaseNo2=9&FileType=1&Lang=C&FolderType=O',
          {header : {'Content-Type' : 'application/json; charset=utf8'}})
        .success(function(data, status, headers, config) {
          $scope.posts = data;
        })
        .error(function(data, status, headers, config) {
        
        });
      });
    </script>
    <meta charset="utf8"/> 
    <title>Filter</title>
  </head>
  <body ng-controller="myController">
    <table>
      <tr >
        <td ng-repeat="a in array">{{a}}</td>
      </tr>
      <tr ng-repeat="p in posts">
        <td>{{p.ID}}</td>
        <td>{{p.departureZh}}</td>
        <td>{{p.ddesc}}</td>
      </tr>
    </table>
  </body>
</html>


執行結果: