问题
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