将ANGULAR与后端请求结合

简单的结合,却是很多应用的基础。RESTFUL就此而生。瘦服务,富客户。

 

将ANGULAR与后端请求结合
<!DOCTYPE html>
<html lang="en" ng-app="app">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Data Binding Example</title>
  </head>
  <body>
    <div ng-controller="main">
      <input type="text" ng-model="firstName" placeholder="firstname">
      <input type="text" ng-model="lastName" placeholder="lastname">
      <table>
        <tr ng-repeat="p in employees">
          <td>{{p.id}}</td>
          <td><span>{{p.first}} {{p.last}}</span></td>
        </tr>
      </table>
    </div>

    <div ng-controller="logger">
      <pre>
        <p ng-repeat="e in events track by $index">{{$index}} - {{e}}</p>
      </pre>
    </div>

    <script src="http://cdn.bootcss.com/angular.js/1.4.8/angular.min.js"></script>

    <script>
      var app = angular.module('app', []);
      app.controller('main', ['$scope', '$http', '$rootScope', function($scope, $http, $rootScope) {
        $scope.employees = [];
        $scope.firstName = $scope.lastName = '';
        $http.get('/employees').success(function(data) {
          $scope.employees = data;
          $rootScope.$emit('log', 'GET /employees success');
        });
      }]);

    app.controller('logger', ['$scope', '$rootScope',function ($scope, $rootScope) {
      $scope.events = [];
      $rootScope.$on('log', function (event, data) {
        $scope.events.push(data.trim());
      });
    }]);
  </script>
  </body>
</html>
将ANGULAR与后端请求结合

将ANGULAR与后端请求结合

将ANGULAR与后端请求结合