In my Angular factory, I have a set of methods for managing a list of objects. One particular method is responsible for making an ajax call to load more items into the list. During this process, I want to display a visual indicator for the "in progress" state of the ajax call (i.e., loading of items). Currently, I am using a simple variable service.isLoading = ...
to handle this by setting it to either true
or false
based on the current state. The challenge with this approach is that the variable becomes accessible outside the factory to any controllers utilizing it.
angular.module('myApp')
.factory('appFactory', function QuestionsServ(...) {
var service = {
list: [],
isBusy: false
};
service.loadItems = function () {
service.isBusy = true;
// make ajax call here
var request = ...
return request.then( function(res) {
service.isBusy = false;
...
}
...
I want to restrict access to this variable within the factory only. Instead of directly manipulating isBusy
, I tried implementing a function like below:
service.checkIsBusy = function () {
return false;
}
However, this also causes issues as now the factory itself thinks it is busy. As a result, I cannot access the status of isBusy
from within loadItems
. What would be the best way to address this dilemma?