CoderBeginner
CoderBeginner

Reputation: 837

how to show error message on view through controller in angular

I have to show an error msg on unique validation check on my html page. I have set error msg on $scope variable. But I am not getting how to display error msg on html page.

This is my controller code where I am setting error msg like:

$scope.error=="Name already in use"

This is my html page where I want to show error message:

<!--I am showing msg like this but the poblem is when the page is being loaded {} comes. -->
<span class="error" ng-show="error"> {{error}}</span>

How to show error msg only when some error msg comes. Please suggest some way for this.

enter image description here

Upvotes: 0

Views: 6624

Answers (2)

Divya MV
Divya MV

Reputation: 2053

from docs

The ngCloak directive is used to prevent the Angular html template from being briefly displayed by the browser in its raw (uncompiled) form while your application is loading. Use this directive to avoid the undesirable flicker effect caused by the html template display.

<span class="error" ng-show="error" ng-cloak> {{error}}</span>

or i guess you have initialized $scope.error='{}' somewhere,remove it if that's the case.

Upvotes: 2

koustubh
koustubh

Reputation: 106

try < span ng-bind="error" >< /span >"

In the case when there is no error you need to reassign a null or an empty value to the error variable of the scope.

Upvotes: 2

Related Questions