1

Hello guys I really need help and advice on this factory and controller issue I am having.

  1. I have a factory that gets data from the server

    sp.factory('homeFeed',['$window','$http','auth',function($window,$http,auth){
    var url = auth.url;
    var version = auth.version;
    var HomeFeed = {};
    
    HomeFeed.getFeeds = function(user){
        //setting variable for get request on home feed
        var req = {
            method: 'GET',
            url: url + version + '/Feed',
            headers: {
                'Content-Type': 'application/json; charset=utf-8',
                'Authorization': 'Bearer ' + token
            },
        }
    
        return $http(req).success(function(res){
            return res;
        });
    };
    
    return HomeFeed;
    }]);
    
  2. controller--

    sp.controller('HomeCtrl',['$scope','homeFeed','$window',function($scope,homeFeed,$window){
    //getting all the home feed data
    $scope.feeds = homeFeed.getFeeds(JSON.parse($window.localStorage['SP-User']))
    
    
    }]);
    

however, after the respond from the server, my view is not updated and the $scope.feeds is not updated as well. Greatly appreciate your help

1
  • I think you should pass a callback to getFeeds, call it inside success and update your $scope.feeds there. Commented Aug 3, 2015 at 20:37

1 Answer 1

1

As you are doing async $http call then that data would not be available at that instance of time. It would be available when ajax call succeeded. You need to use .then function which will create a promise chain and will execute a function when .success function returns a data.

Controller

sp.controller('HomeCtrl',['$scope','homeFeed','$window',
   function($scope,homeFeed,$window){
     //getting all the home feed data
     homeFeed.getFeeds(JSON.parse($window.localStorage['SP-User']))
     .then(function(data){
        $scope.feeds = data 
     });
   }
]);
Sign up to request clarification or add additional context in comments.

Comments

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.