使用$ http角度渲染表格

问题描述:

我在这里做错了什么?我试图使用角度呈现表格,但表格显示为空,其中数据应为{{ place.id}},{{ place.name}}{{ place.social}}

<head> 
    <title>Angular JS </title> 

    <script src="http://code.angularjs.org/1.4.8/angular.js"></script> 
    <script src="http://code.angularjs.org/1.4.8/angular-resource.js"></script> 
    <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.js"></script> 
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> 
    <link rel="stylesheet" type="text/css" href="css/main.css"> 

    <script> 
     var app = angular.module('MyForm', ['ui.bootstrap', 'ngResource']); 
     app.controller('myCtrl', function ($scope, $http) { 
     $http.get('http://passarola.pt/api/places').success(function(data) { 
     $scope.predicate = 'id'; 
     $scope.reverse = true; 
     $scope.currentPage = 1; 
     }; 

     $scope.order = function (predicate) { 
     $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false; 
     $scope.predicate = predicate; 
     }; 

    $scope.totalItems = $scope.places.length; 
    $scope.numPerPage = 5; 
    $scope.paginate = function (value) { 
     var begin, end, index; 
     begin = ($scope.currentPage - 1) * $scope.numPerPage; 
     end = begin + $scope.numPerPage; 
     index = $scope.places.indexOf(value); 
     return (begin <= index && index < end); 
    }; 
    }); 
    </script> 

</head> 

<body ng-app="MyForm"> 
    <div ng-controller="myCtrl"> 
    <div class="container-fluid"> 
     <pre>Passarola Beer</pre> 
     <hr /> 
     <table class="table table-striped"> 
     <thead> 
      <tr> 
      <th class="media_gone"> 
       <a href="" ng-click="order('id')">ID</a> 
      </th> 
      <th><a href="" ng-click="order('name')"> Name</a> </th> 
      <th><a href="" ng-click="order('social')">Social Media</a> </th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr>  
      <td class="media_gone2"> <input type="text" ng-model="search.id" /></td> 
      <td> <input type="text" ng-model="search.name" /> </td>  
      </tr> 
      <tr ng-repeat="place in places | orderBy:predicate:reverse | filter:paginate| filter:search" ng-class-odd="'odd'">  
      <td>{{ place.id}}</td> 
      <td>{{ place.name}}</td> 
      <td class="gender_gone">{{ place.social}}</td> 
      </tr> 
     </tbody> 
     </table> 
     <pagination total-items="totalItems" ng-model="currentPage" 
      max-size="5" boundary-links="true" 
      items-per-page="numPerPage" class="pagination-sm"> 
     </pagination> 
    </div> 
    </div> 
</body> 
</html> 

示例代码:

var app = angular.module('MyForm', ['ui.bootstrap', 'ngResource']); 
    app.controller('myCtrl', function ($scope, $timeout) { 
    $scope.predicate = 'id'; 
    $scope.reverse = true; 
    $scope.currentPage = 1; 
    $scope.order = function (predicate) { 
     $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false; 
     $scope.predicate = predicate; 
    }; 
    $scope.places = [ 
     { id: 'Kevin', name: 25, social: 'boy' }, 
     { id: 'John', name: 30, social: 'girl' }, 
     { id: 'Laura', name: 28, social: 'girl' }, 
     { id: 'Joy', name: 15, social: 'girl' }, 
     { id: 'Mary', name: 28, social: 'girl' }, 
     { id: 'Peter', name: 95, social: 'boy' }, 
     { id: 'Bob', name: 50, social: 'boy' }, 
     { id: 'Erika', name: 27, social: 'girl' }, 
     { id: 'Patrick', name: 40, social: 'boy' }, 
     { id: 'Tery', name: 60, social: 'girl' } 
    ]; 
    $scope.totalItems = $scope.places.length; 
    $scope.numPerPage = 5; 
    $scope.paginate = function (value) { 
     var begin, end, index; 
     begin = ($scope.currentPage - 1) * $scope.numPerPage; 
     end = begin + $scope.numPerPage; 
     index = $scope.places.indexOf(value); 
     return (begin <= index && index < end); 
    }; 
+2

检查浏览器控制台是否有错误。无论何时您看到带有{{}}的原始表达式而不仅仅是空白空间或数据,这都表明角度由于某种原因未能加载。 – Claies

+0

@Claies它说''SyntaxError:失踪)后参数列表和'错误:[$注射器:modulerr]无法实例化模块MyForm由于: [$注射器:nomod]模块'MyForm'不可用!' –

+0

确定,我解决了这部分,但现在它说'错误:$ scope.places是undefined',我不知道该怎么做 –

使用此控制器代码。

app.controller('myCtrl', function ($scope, $http) { 
    $scope.places = []; 
    $http.get('http://passarola.pt/api/places').success(function(data) { 
     $scope.places = data.data; 
     $scope.totalItems = $scope.places.length; 
     $scope.predicate = 'id'; 
     $scope.reverse = true; 
     $scope.currentPage = 1; 
    }; 

    $scope.order = function (predicate) { 
    $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false; 
    $scope.predicate = predicate; 
    };  
$scope.numPerPage = 5; 
$scope.paginate = function (value) { 
    var begin, end, index; 
    begin = ($scope.currentPage - 1) * $scope.numPerPage; 
    end = begin + $scope.numPerPage; 
    index = $scope.places.indexOf(value); 
    return (begin <= index && index < end); 
}; 
}); 
+1

非常感谢,它的工作! –

+0

只是为了知识的目的可以你们发布这个代码的工作演示?谢谢 ! –