简体   繁体   中英

AngularJS ui router (resolve part)

I wrote ui.router to get data from JSON file. I am not getting error messages, but code does not enter "resolve" part. So I cannot get data from JSON file.

Can anyone tell me what could possibly make this happen and the way to fix it?

Here is my code. (function(){

/*
 * 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!!!!');
                    })
                }
            }
        })
     })

})();

I'm pretty sure you have to return the value you want from the $http callbacks:

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');
        });
    }
}

But you should really use .then instead of .success and .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');
        });
    }
}

尝试更改问题:函数($ http)...为

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM