Javascript AngularJS退出递归函数调用

Javascript AngularJS退出递归函数调用,javascript,angularjs,recursion,ionic-framework,ionic,Javascript,Angularjs,Recursion,Ionic Framework,Ionic,我使用以下函数从RESTAPI中提取用户,并按偏移量分页在成功回调时,该函数将使用新的偏移量再次递归调用,以获取下一批用户 问题:如果切换或离开视图,则FetchAttendee函数将一直运行,直到所有用户都被提取。但是,为了提高性能,我想停止为用户获取 fetchAttendees(event_id, offset); function fetchAttendees(event_id, offset) { AttendeeFactory(offset).show({id: event

我使用以下函数从RESTAPI中提取用户,并按偏移量分页
在成功回调时
,该函数将使用新的偏移量再次递归调用,以获取下一批用户

问题:如果切换或离开视图,则
FetchAttendee函数将一直运行,直到所有用户都被提取。但是,为了提高性能,我想停止为用户获取

fetchAttendees(event_id, offset);

function fetchAttendees(event_id, offset) {
    AttendeeFactory(offset).show({id: event_id}, 
        function success(response) {
            [ DO SOMETHING WITH RESPONSE ]
            fetchAttendees(event_id, offset);
        }, 
        function (error) {
        });
}
那么,是否可以停止在查看休假事件上调用fetchAttendee函数

出席者工厂

.factory('AttendeeFactory', function ($resource) {
    return function (offset) {
        return $resource('http://10.0.0.6:8000/backend/attendees/:id/', {}, {
            show: { method: 'GET', headers: {'attendee-offset': offset}, isArray: true }
        });
    };
})
以下是伪代码(未测试您想要做的事情)

如果[DO SOMETHING WITH RESPONSE]包括将其绑定到视图的作用域,那么您必须添加代码,让服务通知控制器数据已更改

在这种情况下,您可以使用$rootScope、$on和$emit在获取与会者时从服务发出消息,以便控制器可以侦听并更新消息。下面是一个简单的例子:

// in the controller
$rootScope.$on("AttendeeFetchedEvent", function($event, data){
  // do something with data
});

// in the factory/service
$scope.$emit("AttendeeFetchedEvent", dataToSendToController);

我添加了源代码。你的方法适用于这家工厂吗?当然。实际上,我会将fetchAttendes移动到工厂(服务)中,然后从控制器调用它。我马上就要上路了,回家后我会告诉你怎么做。我编辑了我的回复,告诉你怎么做你需要的事情。请注意,我将所有与服务相关的代码移到了控制器使用的工厂中。非常感谢!我要去看看。效果很好!!再次感谢您的帮助;)
// in your controller
app.controller('YourController', ['$scope', 'AttendeeFactory', function($scope, AttendeeFactory) {

    ...
    AttendeeFactory.fetchAttendees(event_id, offset);
    ...

}]);



// in the state change handler that matches leaving your view
AttendeeFactory.pause();


// in your service/factory
app.factory('AttendeeFactory', function($resource) {
    var isPaused = true; 

    function fetchAttendees(event_id, offset) {
        isPaused = false;
        fetchAttendeesRecursive(event_id, offset);
    }

    function fetchAttendeesRecursive(event_id, offset) {
        if (!isPaused) {
            Attendee(offset).show(
                {id: event_id}, 
                function success(response) {
                    [ DO SOMETHING WITH RESPONSE ]
                    fetchAttendees(event_id, offset);
                }, 
                function (error) {}
           );
        }
    }

    function Attendee(offset) {
        return = $resource(
            'http://10.0.0.6:8000/backend/attendees/:id/',
            {},
            {
                show: {
                    method: 'GET', 
                    headers: {'attendee-offset': offset}, 
                    isArray: true
                }
            }
        );
    }

    function pause() { isPaused = true; }

    return {
        fetchAttendees: fetchAttendees,
        pause: pause
    };
});
// in the controller
$rootScope.$on("AttendeeFetchedEvent", function($event, data){
  // do something with data
});

// in the factory/service
$scope.$emit("AttendeeFetchedEvent", dataToSendToController);