I'm running into an unusual behaviour with ng-switch
that I'm not able to figure out. At the top of my page, I have
#content{"ng-switch" => "root.showAlert"}
%div.alert.ng-cloak#alert{"ng-switch-when" => "true"}
{{root.alert.message}}
%div.close
%a{:href => "#", "ng-click" => "dismissAlert()"}
=image_tag "icons/icon-close-black.png"
In one of my controllers (highest level) I have the following action
$scope.displayAlert = function(message) {
$scope.root.alert = {
message: message
};
$scope.root.showAlert = true;
if (!$scope.$$phase) {
$rootScope.$digest();
}
}
$scope.root is defined in the $rootScope so is accessible to everything.
When I change the root.showAlert
flag to true, I am expecting to see the alert appear as it should be watching this variable, however it isn't happening immediately, and instead showing when I change something else in the app by performing any other action.
By adding my $rootScope.$digest()
it works and displays immediately, but I was wondering why it won't do it on its own?
Dismissing:
$rootScope.dismissAlert = function() {
$scope.root.showAlert = false;
delete $scope.root.alert;
}