Angularjs – Restangular call inside factory/service

Issue

I am using factory as follows:

var appModule = angular.module('appModule', ['ngRoute','restangular']);

appModule
  .config(['$routeProvider', function($routeProvider) {
    $routeProvider
      .when('/home', {
        templateUrl: 'home.html',
        controller:  'ngHomeControl'
      })
      .when('/contacts', {
        templateUrl: 'contacts.html',
        controller:  'ngContactControl'
      });
  }]);

appModule
  .factory('testFactory', function testFactory(Restangular) {
    return {
      getFriendList: function() {
        return Restangular
          .all('api/users/getfriendsinvitations/')
          .getList()
          .then(function(response) {
            //
          });
      } 
    }
  });

appModule
  .controller('ngHomeControl', function($scope, testFactory) {
    $scope.homeFriends = testFactory.getFriendList();
  });

appModule
  .controller('ngContactControl', function($scope, testFactory) {
    $scope.contactFriends = testFactory.getFriendList();
  });

Here I can’t get the value for $scope.homeFriends & $scope.contactFriends from the Restangular call. Can anyone suggest to me how to get values from Restangular call using factory/service?

Solution

Finally I found:

appModule
  .factory('testFactory', ['Restangular', function(Restangular) {
    return {
      getFriendList: Restangular
        .all('api/users/getfriendsinvitations/')
        .getList();
    }
  }]);

testFactory.getFriendList.then(function(homeFriends) {
  $scope.homeFriends = homeFriends;
}

Answered By – Hulk1991

Answer Checked By – Clifford M. (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.