2

I have a angular service function that is being called multiple times. In the index.html page I have the following line:

<li><i class="pull-right"></i><br/>{{appCtrl.service.getCurrentUser()}}&nbsp;</li>

In the application controller I set the variable

appCtrl.controller('AppController', function ($state, securityService, $log) {

        $log.info('App controller');

        var appCtrl = this;
        appCtrl.service = securityService;
});

In my service I exposed the function

   login.factory('securityService', function ($window, $log) {

    var currentUser;

    return {
        setCurrentUser: function (user) {
            currentUser = user;
            $window.sessionStorage.setItem('User', JSON.stringify(currentUser));
        },
        getCurrentUser: function () {
            $log.info('Calling current user');
            if (!currentUser) {
                var storedObject = $window.sessionStorage.getItem('User');
                currentUser = JSON.parse(storedObject);
            }
            return currentUser;
        }
    }
});

The following line in the getCurrentUser function gets called multiple times when the application starts up or page refresh is being done.

$log.info('Calling current user');

The controller is being called only once, I monitor it by looking at $log.info('App controller');

Is it being called as part of the dirty checking process or am I doing something wrong?

2 Answers 2

5

Angular calls your function on every digest cycle, you can set breakpoint inside the function and check it. If you are on 1.3 version, then please take a look at One Time Binding feature. If not then call the service inside the controller and bind view to some scope variable:

$scope.currentUser = securityService.getCurrentUser();

And inside view bind to scope variable:

{{currentUser}}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, the problem with setting the function to a variable on the app controller is that the app controller sets the variable to the initial value with app startup (which is undefined). When the setCurrentUser on the factory gets called from the login controller the variable in the app controller doesn't get updated.
Then I would use some standard event mechanism (all this $broadcast/$emit/$on stuff) to update other controllers when user value updated by login controller.
Thank you udalmik, I have gone for the $broadcast and $on strategy. It works perfectly for my scenario.
0

Try this, this is correct factory declaration. Because internally AngularJS calls yout factory like: securityService(injects); , each time you inject (use) your factory.

login.factory('securityService', function ($window, $log) {
        var currentUser;
        return {

            setCurrentUser: function (user) {
                currentUser = user;
                $window.sessionStorage.setItem('User', JSON.stringify(currentUser));
            },
            getCurrentUser: function () {
                $log.info('Calling current user');
                if (!currentUser) {
                    var storedObject = $window.sessionStorage.getItem('User');
                    currentUser = JSON.parse(storedObject);
                }
                return currentUser;
            }  
        };

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.