使用AngularJS 1.2打破简单的restangular示例

时间:2014-12-19 09:28:27

标签: angularjs restangular

我正在执行一个基本的Restangular示例,它适用于AngularJS 1.1,但是在1.2上,REST请求已发送,数据已收到,但是表格显示不正确。

我在这里阅读了一些关于从1.1 to 1.2升级的线程,但我没有看到问题,因为示例非常简单,并没有明确使用ngRoute,隔离范围或自定义指令。

HTML

<!DOCTYPE html>

<html ng-app="countries">

  <head>
    <meta charset="utf-8" />
    <title>REST Country Example</title>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
    <script src="//code.angularjs.org/1.2.27/angular.min.js"></script>
    <script src="//code.angularjs.org/1.2.27/angular-resource.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
    <script src="//cdn.rawgit.com/mgonto/restangular/master/dist/restangular.min.js"></script>

    <script src="app.js"></script>
  </head>

  <body ng-controller="mainCtrl">
    <div>
    <h2>Restangular Example with RESTCountries.eu</h2>


      <input type="text" ng-model="search" class="search-query" placeholder="Search">
      <table class="table table-responsive table-striped">
        <thead>
        <tr>
          <th>Country</th>
          <th>Capital</th>
          <th>Code</th>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="country in countries | filter:search | orderBy:'name'">
          <td>{{country.name}}</td>
          <td>{{country.capital}}</td>
          <td>{{country.alpha2Code}}</td>
        </tr>
        </tbody>
      </table>

  </div>

  </body>

</html>

JS

app = angular.module('countries', ['restangular']);
app.config(function(RestangularProvider) {
      RestangularProvider.setBaseUrl('http://restcountries.eu/rest/v1');
  });

app.controller('mainCtrl', function($scope, Restangular) {
  $scope.countries = Restangular.all('all').getList();
});

1.1.5 Plunk (working)

1.2.27 Plunk (not working)

知道什么是缺失/不正确的,以便在1.2上正常工作?

干杯,

1 个答案:

答案 0 :(得分:1)

我之前从未使用过restangular。看起来它返回1.2版本的承诺,而不是数据,我能够通过这个小修改加载数据:

app.controller('mainCtrl', function($scope, Restangular) {
  Restangular.all('all').getList().then(function(result) {
    $scope.countries = result;
  });
});

Plunker