Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs: ng-show / ng-hide expression not evaluated

Tags:

angularjs

I'm new to both javascript and AngularJS, and wondering why is the expression inside the quotes not evaluated?

<span ng-show="{{remaining()}}!==0">sometext</span>

it is simply printed like this:

<span ng-show="2!==0">sometext</span>

and the ng-show is not working regardless of the contents. The text ( and the printed expression) is shown even if the expression is wrapped in an eval, :

eval("{{remaining()}}!==0")

I resorted to creating a function in my controller for this:

<span ng-show="renderOrNot()">sometext</span>

which works, but I would prefer not to have to write a function each time I want to make a comparison

like image 713
jsaddwater Avatar asked Jul 03 '13 09:07

jsaddwater


People also ask

What is difference between ng-show and Ng-hide?

The ng-show and ng-hide both are directive . The difference between is : ng-show directive will show the html element if expression resilts is true and ng-hide directive hide the html element if expression result is true .

What is Ng-hide in AngularJS?

AngularJS ng-hide Directive The ng-hide directive hides the HTML element if the expression evaluates to true. ng-hide is also a predefined CSS class in AngularJS, and sets the element's display to none .

Which is the correct syntax of AngularJS directive ng-show?

The ng-show Directive in AngluarJS is used to show or hide the specified HTML element. If the given expression in the ng-show attribute is true then the HTML element will display otherwise it hides the HTML element. It is supported by all HTML elements.

Which of the following directives should you use to show an image based on account status in AngularJS?

The ng-src directive should be used instead of src if you have AngularJS code inside the src value. The ng-src directive makes sure the image is not displayed wrong before AngularJS has evaluated the code.


2 Answers

Almost there ...

When you use {{}}, the values are interpolated, i.e. the markup is replaced with the result of the expression. ngShow expects only the expression, so just use the function as it is, and it will work:

<span ng-show="remaining() !== 0">sometext</span>

In general, you'll only want {{ }} when your expression / content should be displayed.

like image 109
Narretz Avatar answered Oct 04 '22 21:10

Narretz


You must not use it {{}} because your value is bind. Use ng-show like this:

<span ng-show="remaining() !== 0">sometext</span>
like image 20
EpokK Avatar answered Oct 04 '22 22:10

EpokK