The code block below waits for a timeout, then performs an HTTP request with a promise, then changes an Angular Material scope variable based on the response. It is called during with an ng-change
on an input field. I also have an element with ng-hide="showStuff"
in my HTML, and obviously, when $scope.showStuff
becomes false, I want it to disappear right away. Unfortunately, it doesn't actually disappear until I select something else in the DOM.
I've used promises before to change things in the DOM, and those work fine. Why doesn't the DOM update on its own, and how do I work around it?
$scope.checkSomething = function() {
// Use a timeout to prevent a checks from going off too rapidly
if (queryTimeout) {
clearTimeout(queryTimeout);
}
queryTimeout = setTimeout(function() {
bluebird.bind({}).then(function() {
return makeHttpRequest();
}).then(function(res) {
$scope.showStuff = res.data.length > 0;
})
}, 500);
}