0

i am getting a JSON file from local host, i successfully get the data, i can see it when i console log, but when i inject the Factory in my controller it returns a null object, that means the variable erroMessages does not get the JSON object because the success function is not being executed before return in the Factory,please help.

    assetInModule.factory('helperFactory',function($http,$sce){
    'use strict';
    var title = "default";
    var errorMessages = {content:null};

    return {

        title : function(){ return title;},

        setTitle : function(newNitle){title = newNitle;},

        getErrorMessages : function(){

             $http.get('http://127.0.0.1/assetinspa/public/js/helpers/error_messages.json')
                  .success(function(data){

                    errorMessages = data;

                  });

                 return errorMessages; 
        }
    };
});

2 Answers 2

2

try

assetInModule.factory('helperFactory',function($http,$sce){
'use strict';
var title = "default";
var errorMessages = {content:null};

return {

    title : function(){ return title;},

    setTitle : function(newNitle){title = newNitle;},

    getErrorMessages : function(){
         return $http.get('http://127.0.0.1/assetinspa/public/js/helpers/error_messages.json');
    }
};
});

angular.controller('YourCtrl', function ($scope, helperFactory) { 
  $scope.errorMessages = {};
  
  helperFactory
    .getErrorMessages()
    .success(function(data) {
      $scope.errorMessages = data;
    });
});

return is fired before $http.success end

Sign up to request clarification or add additional context in comments.

2 Comments

It returns d {$$state: Object} when i console log, can i get the direct object, because it returns this object,its the first me seeing it
Thank you i overlooked the last code segment, it worked,its the first time getting help on stackOverflow
1
    "use strict";
assetInModule.factory("helperFactory", ["$http","$q",
    function ($http, $q) {


        var getErrorMessages = function() {
            var deferred = $q.defer();
            $http.get('http://127.0.0.1/assetinspa/public/js/helpers/error_messages.json')
                .success(function(data) {
                    deferred.resolve(data);
                });

            return deferred.promise;
        };

        return {

            title: function () { return title; },

            setTitle: function (newNitle) { title = newNitle; },
            GetErrorMessages: getErrorMessages
        }
    }
]);

At the time of use

helperFactory.GetErrorMessages().then(function(result){
//get the data
})

2 Comments

I am sorry but i am learning, then how do i get the object in my controller? which property must in call
don't use custom promise using $q(BAD PATTERN), utilize promise return by $http get method..

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.