Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does {{ isNan(x) }} not work in angularJS?

Tags:

angularjs

Note the title of this question has changed to one that's more focused to the exact problem. See the comment stream to follow how it came about that I realized the problem is that AngularJS does not seem to handle {{ isNaN() }}

I have the following in my HTML:

xx {{ option.selectedSubject }} yy {{ (option.selectedSubject == null) }} zz

and I also tried:

xx {{ option.selectedSubject }} yy {{ option.selectedSubject == null }} z

and:

xx {{ option.selectedSubject }} yy {{ option.selectedSubject === null }} zz

Can someone help explain to me why I get the following for each of the above when I view my page:

xx null yy false zz

Update 1 I tried the following:

aa {{ option.selectedSubject === "null" }} bb {{ option.selectedSubject == "null" }} cc

and it gives this:

aa false bb false cc

Update 2 I am not sure if this helps but here's what populates the values of option.selectedSubject. In this case there's nothing in local storage:

$scope.option.selectedSubject = parseInt(localStorage.get('selectedSubject'));

When I check the value of $scope.option.selectedSubject it is NaN

like image 746
Samantha J T Star Avatar asked Dec 19 '13 17:12

Samantha J T Star


People also ask

How does angular handle NaN?

However, initially, when the text input is empty it returns NaN. How can I prevent this from happening with Angular? You have to watch and convert the datatype of amount to integer otherwise its string multiplying with 10 which will give you unexpected results.

What is the meaning of NaN in angular?

NaN in Typescript stands for Not a Number. It is the result of numerical operations, where result is not a number . It is the property of the global object.

What is AngularJS used for?

AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets you extend HTML's syntax to express your application's components clearly and succinctly. AngularJS's data binding and dependency injection eliminate much of the code you would otherwise have to write.

What is the use of isNaN function?

The isNaN() function is used to check whether a given value is an illegal number or not. It returns true if value is a NaN else returns false. It is different from the Number.


2 Answers

The problem here is not isNaN. The problem is that you are calling a function on the $scope variable that does not exist.

Every interpolated piece of text ( '{{ text }}' ), is associated to a $scope variable and evaluated against that $scope variable.

To make things easy for you, $scope.property can be created on the fly if it doesn't exist. Anytime you use ngModel='someProperty' or {{ aPropertyName }}, then the corressponding $scope.someProperty or $scope.aPropertyName is created for you automatically if it didn't already exist. This shortcut only works with primitives, however.

Function calls are always evaluated against $scope, and never created. This is why you have to say $scope.isNaN = isNaN, as you found in your earlier comment.

Try it with any function. isArray, isNumber, etc. It won't work unless you have put a $scope.functionName = functionName in a controller somewhere.

Edit: Also, if you REALLY want to do the isNaN test right in the interpolation, you can take advantage of javascript's type system and use

{{ property != property }}

But this is bad form...

http://plnkr.co/edit/wfLqzk8QxScsJPF5qY12?p=preview

like image 154
Noishe Avatar answered Oct 10 '22 20:10

Noishe


Edit : The original question was Can I have a test inside {{ }} in angularJS?, and $scope.option.selectedSubject was meant to be null and not NaN. The author should have open another question instead of changing it to a complete different one.


Answer to the original question :

You can put an expression inside your binding, hence you can test {{ option.selectedSubject == null }}. Check your scope if the result of the evaluation is not what you are expecting.

like image 3
Michael P. Bazos Avatar answered Oct 10 '22 19:10

Michael P. Bazos