Why is $scope >= 0 showing true in interpolation while empty in controller?

15 views Asked by At

I'm setting a $scope variable to 0 or 1 based on a successful action and showing elements with ng-show. I'm resetting the $scope variable to empty when the step is finished. However, I have discovered that $scope = '' is shown as true if in interpolation {{$scope >= 0}}. Why not false?

controllers.Ctrl = function ($scope, $rootScope, $timeout, $filter, ajax, $parse) {

$scope.datachanged = '';
console.log($scope.datachanged >= 0); // true

};
1

There are 1 answers

0
Rory LM On

That's just javascript for you. '' >= 0 will evaluate to true. This is because the empty string '' is falsy, and so is 0, so you can think of it as false >= false.

Instead, why don't you reset the variable to undefined (ie $scope.datachanged = undefined). undefined >= 0 will evaluate to false.

Alternatively, if the variable will only be set to 0 or 1, you could also just check to see if your variable equals 1 where you do the interpolation. {{datachanged === 1}