Angularjs - 工厂/服务内部的重新调用 [英] Angularjs - Restangular call inside factory/service

查看:35
本文介绍了Angularjs - 工厂/服务内部的重新调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 factory 如下:

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

在这里我无法获得 $scope.homeFriends 的值 &$scope.contactFriends 来自 Restangular 调用.谁能建议我如何使用 factory/serviceRestangular 调用中获取值?

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?

推荐答案

我终于找到了:

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

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

这篇关于Angularjs - 工厂/服务内部的重新调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆