Angular ng-repeat仅显示第一项

时间:2014-08-06 09:14:22

标签: asp.net-mvc angularjs api

我有下表:

<div data-ng-app="myApp">
    <div data-ng-controller="MyCtrl">
        <form>
            <table>
                <tr>
                    <td><b>ID</b></td>
                    <td><b>Name</b></td>
                    <td><b>Surname</b></td>
                    <td><b>House</b></td>
                    <td><b>Address</b></td>
                    <td><b>Locality</b></td>
                    <td><b>Contact1</b></td>
                    <td><b>Contact2</b></td>
                    <td><b>Contact3</b></td>
                    <td><b>Reply</b></td>
                </tr>
                <tr><td></td></tr>
                <tr ng-repeat="telesale in telesales">
                    <td>{{telesale.ID}}</td>
                    <td>{{telesale.Name}}</td>
                    <td>{{telesale.Surname}}</td>
                    <td>{{telesale.House}}</td>
                    <td>{{telesale.Address}}</td>
                    <td>{{telesale.Locality}}</td>
                    <td>{{telesale.Contact1}}</td>
                    <td>{{telesale.Contact2}}</td>
                    <td>{{telesale.Contact3}}</td>
                    <td>{{telesale.Reply}}</td>
                </tr>
            </table>
        </form>
    </div>
  </div>

角:

 <script type="text/javascript">
      var myApp = angular.module('myApp', []);

      myApp.controller('MyCtrl',['$scope', '$http', function ($scope, $http) {

          GetPersons();

          function GetPersons() {
            $http({
                method: 'GET',
                url: '/api/data'
            }).
            success(function (data) {
                if(data != null || data != 'undefined') {
                    $scope.telesales = data;
            }
          })
          .error(function (error) {
             $window.alert("Unable to retrieve people" + error.message);
          });
          }

      } ]);
  </script>

从api检索的数据是一个包含40行计数的列表(作为要验证的断点),但是只显示列表中的行。为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

当您的web api控制器返回HttpResponseMessage消息而不是纯数据时,可能会发生这种情况,在这种情况下,您需要使用result.data获取数据,即:

 <script type="text/javascript">
      var myApp = angular.module('myApp', []);

      myApp.controller('MyCtrl',['$scope', '$http', function ($scope, $http) {

          GetPersons();

          function GetPersons() {
            $http({
                method: 'GET',
                url: '/api/data'
            }).
            success(function (result) {
                if(result.data != null || result.data != 'undefined') {
                    $scope.telesales = result.data;
            }
          })
          .error(function (error) {
             $window.alert("Unable to retrieve people" + error.message);
          });
          }

      } ]);
  </script>