2016-07-15 16 views
1

このコードがangularjs 1.2.0-rc.2ではうまく動作するが、それ以降のバージョンではうまくいかない理由は分かりません(私は1.2.0、1.4.9、1.5.7で試しました)角度バージョンの問題と約束

index.htmlを

<body ng-app="MyApp"> 
    <h1>Open Pull Requests for Angular JS</h1> 
    <ul ng-controller="DashboardCtrl"> 
    <li ng-repeat="pullRequest in pullRequests"> 
     {{ pullRequest.title }} 
    </li> 
    </ul> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.js"></script> 
    <!--<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.js"></script>--> 
    <!--<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>--> 
    <!--<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>--> 
    <script src="scripts/app.js"></script> 
</body> 

スクリプト/私は約束を再されていることを確認することができ、デバッグ

'use strict'; 

var app = angular.module('MyApp', []); 

app.controller('DashboardCtrl', ['$scope', 'GithubService',function($scope, GithubService) { 
    $scope.pullRequests = GithubService.getPullRequests(); 
}]); 

app.factory('GithubFactory', ['$q', '$http',function($q, $http) { 
    var myFactory = {}; 
    myFactory.getPullRequests = function() { 
     var deferred = $q.defer(); 
    $http.get('https://api.github.com/repos/angular/angular.js/pulls') 
      .success(function(data) { 
      deferred.resolve(data); // Success 
      }) 
      .error(function(reason) { 
      deferred.reject(reason); // Error 
      }); 

     return deferred.promise; 
    } 

    return myFactory; 

}]); 

をapp.js解決されましたが、データは表示されません... 約束を使用する正しい方法は何ですか?

答えて

2

1.2の約束事はテンプレートで自動的に "展開"されないので、うまくいきません。あなたは明示的に解決されたデータを設定する必要があります。

$scope.pullRequests = GithubService.getPullRequests(); 

そして、次のようになります:これは間違ってい

GithubService.getPullRequests().then(function(data) { 
    $scope.pullRequests = data; 
}); 

そして、もう一つ。

app.factory('GithubFactory', ['$http', function($http) { 
    var myFactory = {}; 
    myFactory.getPullRequests = function() { 
     return $http.get('https://api.github.com/repos/angular/angular.js/pulls') 
      .then(function(response) { 
       return response.data; 
      }); 
    }; 
    return myFactory; 
}]); 
+0

うん:$httpサービスは、すでにあなたのための1を返しますので、あなたはdeferredオブジェクトとの約束を構築するべきではありません! Promise auto-unwrapは、1.2.0-rc.3以降で廃止されました。https://docs.angularjs.org/guide/migration#angular-expression-parsing-parse-interpolate- – electblake