A
A
Artem Shchurin2016-04-12 10:52:32
JavaScript
Artem Shchurin, 2016-04-12 10:52:32

Why does the factory keep the old value of the variable when accessed outside the angular context?

Good afternoon!
Have a factory

app.factory('headerMode', ['modal2', function(modal2) {
    var _mode = 'init';
    return {
      setMode: function(mode) {
        _mode = mode;
      },
      checkHistory: function() {
        if(_mode === 'history'){
          modal2.show();
        }
      },
      checkParameters: function(){
        return _mode === 'parameters'
      }
    }
  }])

D controller hangs $watchwhen the variable is changed, it calls the above- setModepresented factory, the factory itself serves to exchange the state of a single component, setModeit is called only in the 1st place.
If you inject the factory to another controller, and call checkHistoryor checkParameterseverything is fine, the value is _modeexactly what came in setModeearlier, or 'init'if the state did not change.
But if called outside the context of angular
angular.injector(['ng', 'myapp']).get('headerMode').checkHistory()

or
angular.injector(['ng', 'myapp']).get('headerMode').checkParameters()

inside the factory _modeis always in state 'init', even if we changed it via _setMode
Is a new instance of the factory created for my call out of context?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question