I have some content on my page which is wrapped in an ng-if like below:
<div ng-if="ShowMessgae" class="text-danger">
<p>
<strong>
Message displayed to User
</strong>
</p>
</div>
Then in my angular js controller I have the following:
function MyController($scope, $q, notifyHelper) {
openAjaxLoaderGif();
$scope.ShowMessgae = false;
MyService.getVarsFromSession().then(function (result) {
// some code removed for brevity
if(some coditional) {
$scope.ShowMessgae = true;
}
}, function (data, failReason, headers) {
notifyHelper.error("Error has occurred - try again");
})
})
.finally(function () {
closeAjaxLoaderGif();
});
};
I am setting the value of ShowMessage to false and it only is true if a certain condition is met. In the majority of instances ShowMessage will be false. However this results in when the page loads the markup Message displayed to User briefly renders and then it disappears (to show what I expect) - is there something I am doing wrong which is causing this flicker?
This happens because the content is rendered until AngularJS detects that ShowMessgae
is false
and then hides it.
In order to not show the content at all until angular is ready to "say" whether ShowMessgae
is true
or false
you should use ng-cloak
. This way the content is not shown until AngularJS "knows" the value of ShowMessgae
. Then, at this point, the framework is ready to show (or not) the content, depending on the value of ShowMessgae
<div ng-if="ShowMessgae" class="text-danger" ng-cloak>
<p>
<strong>
Message displayed to User
</strong>
</p>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With