0

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

})();

2
  • Try adding a success method to the $http promise that returns the data from the response. Commented Aug 18, 2016 at 17:58
  • Also, using .success and .error for $http promises is deprecated. I would recommend using .then Commented Aug 18, 2016 at 17:59

2 Answers 2

1

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');
        });
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

try to change question: function ($http)... to

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

1 Comment

just using function($http) {} should work just fine since the parameter $http is the same as the actual name of the $http service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.