2016-08-18 18 views
0

JSONファイルからデータを取得するためにui.routerを書きました。 エラーメッセージは表示されませんが、コードは「解決」部分には入力されません。 JSONファイルからデータを取得できません。AngularJS ui router(解決部分)

誰も私にこのことが起こる可能性があり、それを修正する方法を教えてもらえますか?

ここに私のコードです。 (関数){

/* 
* Declaration of main angular module for this application. 
* 
* It is named turtleFacts and has no dependencies (hence the 
* empty array as the second argument) 
*/ 
angular 
    .module('GrammarQuiz', ['ui.router']) 
    .config(
     function($stateProvider, $urlRouterProvider) { 
     console.log('HOLY SMOKES, I CAN BREATHE IN HERE') 
     $urlRouterProvider.otherwise('/browse/1'); 
     $stateProvider 
      .state('home',{ 
      url: "/#/testtest", 

      resolve: { 
       questions: function($http){ 
        console.log('!!#@!#@!'); 
        return $http({ 
         method: 'GET', 
         url: 'api/data1.json' 
        }).error(function(data,status,headers,config){ 
         console.log('thisi now working!!!!'); 
        }) 
       } 
      } 
     }) 
    }) 

})();

+0

$ http約束事に成功メソッドを追加して、スポンサー。 – csschapker

+0

また、$ http約束事のために.successと.errorを使用することは推奨されません。 .then – csschapker

答えて

1

私はあなたが$にhttpコールバックから必要な値を返す必要が確信している:

resolve: { 
    questions: function($http) { 
     return $http({ 
      method: 'GET', 
      url: 'api/data1.json' 
     }).success(function(data) { 
      return data; // this is what you're missing 
     }).error(function() { 
      console.log('error'); 
     }); 
    } 
} 

しかし、あなたが本当に代わり.successの.then使用する必要があると.ERROR

resolve: { 
    questions: function($http) { 
     return $http({ 
      method: 'GET', 
      url: 'api/data1.json' 
     }).then(function success(response) { 
      return response.data; 
     }, function error(response) { 
      console.log('error'); 
     }); 
    } 
} 
0

質問を変更してみよう:関数($ http)...

question: ['$http', fucntion($http) {..}] 
+0

を使用することをお勧めします。 'function($ http){}'は、パラメータ '$ http 'が$ httpサービスの実際の名前と同じであるため、うまく動作するはずです。 – csschapker

関連する問題