Angularjs-重新启动工厂/服务内部的Angular调用
我使用的Angularjs-重新启动工厂/服务内部的Angular调用,angularjs,scope,angularjs-service,restangular,angularjs-factory,Angularjs,Scope,Angularjs Service,Restangular,Angularjs Factory,我使用的工厂如下: var appModule = angular.module('appModule', ['ngRoute','restangular']); appModule .config(['$routeProvider', function($routeProvider) { $routeProvider .when('/home', { templateUrl: 'home.html', controller: 'ngH
工厂
如下:
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();
});
在这里,我无法从restanglar
调用中获取$scope.homeFriends
和$scope.contactFriends
的值。有谁能建议我如何使用工厂/服务从重新启动的调用中获取值吗?最后我发现:
appModule
.factory('testFactory', ['Restangular', function(Restangular) {
return {
getFriendList: Restangular
.all('api/users/getfriendsinvitations/')
.getList();
}
}]);
testFactory.getFriendList.then(function(homeFriends) {
$scope.homeFriends = homeFriends;
}
由于getFriendList方法返回承诺,您可能需要使用然后回调绑定到列表。