Answer the question
In order to leave comments, you need to log in
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'
}
}
}])
$watch
when the variable is changed, it calls the above- setMode
presented factory, the factory itself serves to exchange the state of a single component, setMode
it is called only in the 1st place. checkHistory
or checkParameters
everything is fine, the value is _mode
exactly what came in setMode
earlier, or 'init'
if the state did not change. angular.injector(['ng', 'myapp']).get('headerMode').checkHistory()
angular.injector(['ng', 'myapp']).get('headerMode').checkParameters()
_mode
is always in state 'init'
, even if we changed it via _setMode
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question