2016-08-18 219 views
0

我寫了ui.router從JSON文件中獲取數據。 我沒有收到錯誤消息,但代碼沒有輸入「解決」部分。 所以我無法從JSON文件中獲取數據。AngularJS ui路由器(解析部分)

任何人都可以告訴我什麼可能會發生這種情況和解決辦法嗎?

這是我的代碼。 (功能(){

/* 
* 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 promise添加成功方法,該方法返回re sponse。 – csschapker

+0

另外,對$ http promises使用.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和.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