12

I am scratching my head here. I am using angularJS and trying to use the expression that contains call to parseInt.

{{0 == 2}}  

...prints out false as expected.)

However, when I am trying:

{{parseInt(0) == parseInt(2)}} 

... it prints out... true !

How can this be possible?

Charlie
  • 22,886
  • 11
  • 59
  • 90
jazzblue
  • 2,411
  • 4
  • 38
  • 63

5 Answers5

13

Angular does not use JavaScript's eval() to evaluate expressions. Instead Angular's $parse service processes these expressions.

Angular expressions do not have access to global variables like window, document or location. This restriction is intentional. It prevents accidental access to the global state – a common source of subtle bugs.

Refer

In your html

Both parseInt(0) and parseInt(2) are undefined in your html.

So {{undefined==undefined}} is true.Beacause parseInt is a Javascript function.So you cant access the parseInt function in side {{}}. [Here parseInt is not a scope variable]

Solution

If you wish to do this,

define parseInt in your controller,

$scope.parseInt = parseInt;

Then you can use the parseInt method in your html

Community
  • 1
  • 1
Muhsin Keloth
  • 7,855
  • 7
  • 39
  • 59
7

That's because parseInt is not defined in your scope.

http://jsfiddle.net/halirgb/Lvc0u55v/

CD..
  • 72,281
  • 25
  • 154
  • 163
  • what you mean by parseInt is not defined? – Umer Hayyat Jun 15 '16 at 04:52
  • can you see parseInt here w3schools.com/jsref/jsref_parseint.asp ? – Umer Hayyat Jun 15 '16 at 04:54
  • 2
    He means "parseInt()" as a function is not defined in your scope. `parseInt` is a Javascript function, but within {{ }}, Angular doesn't consider that as Javascript. It maps it to the scope of your controller. Therefore `undefined == undefined` equals true. – Bjorn 'Bjeaurn' S Jun 15 '16 at 05:50
2

You can't execute regular JS in an angular expression. Your expressions will be evaluated against the current scope. So, parseInt is undefined in the current scope.

If you set parseInt as a function reference, it will work.

$scope.parseInt = parseInt;
Charlie
  • 22,886
  • 11
  • 59
  • 90
1

This is because the view is attached to the controller via scope.

So whatever we write in view either a variable or a function or anything it's rendered by appending $scope in front of it.

eg. a is rendered as $scope.a in the view

So when we write parseInt, its rendered by $scope.parseInt which is not defined.

FIX- define $scope.parseInt = parseInt in the controller attached to the particular view

Rohit
  • 221
  • 1
  • 7
-1

You have comparing both undefined values so result will be true.

You cannot call a javascript method(parseInt) via angular directives(ng-blur,ng-change,..) either you can achieve by making angular functions.

Solution 1:

{{0*1 == 2*1}} 

Just do a trick to convert to Integer by multiply with 1 (0*1 = 0, 2*1 =1).

Solution 2:

{{parseInt(0) == parseInt(2)}} 

Controller:

// To Convert specific format
$scope.parseInt  = funtion(value){
    return parseInt(value,10);
}

or

$scope.parseInt  = parseInt;

reference here

Aravinthan K
  • 1,763
  • 2
  • 19
  • 22
  • Do elaborate more on the answer, and why it is correct answer, failing which it will be deleted. – Mamta D Jun 15 '16 at 11:25
  • Although this code may answer the question, providing additional context regarding _why_ and/or _how_ it answers the question would significantly improve its long-term value. Please [edit] your answer to add some explanation. – Toby Speight Jun 15 '16 at 20:48
  • Thank you guyz!! for correcting me :-) – Aravinthan K Jun 16 '16 at 05:24