I am trying to bind to a boolean variable on my controller's scope to indicate whether or not a loading animation should be displayed. However, the following code does not work. The function inside $timeout
runs, but the view is not updated.
When I inspect the scope using the chrome angular extension, I see that ctrl.loading
is true
.
Is this because of booleans being value types in javascript, as opposed to reference types?
My guess is that the view is literally binding to true
, and not to the location of a boolean value which would change.
How do I get the view to bind to the variable, and not to the value, the variable has initially?
controller:
function TestController($scope,$timeout){
"use strict";
var loading=true;
$timeout(function(){
loading=false;
}, 1000);
return {
loading:loading
}
}
template:
<div>
<h1 ng-show="ctrl.loading">Loading</h1>
<h1 ng-hide="ctrl.loading">Not Loading</h1>
</div>
The abovecode is just an example, really I would set the value after a get request wqas returned.
$http.get().then(function() {
loading=false;
}, function() {
loading=false;
})
but the effect is the same.