Angularjs - Restangular call inside factory/service

本小妞迷上赌 提交于 2019-12-22 17:18:41

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/22496041/angularjs-restangular-call-inside-factory-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!