为什么{{isNan(x)}}在angularJS中不起作用? [英] Why does {{ isNan(x) }} not work in angularJS?

查看:116
本文介绍了为什么{{isNan(x)}}在angularJS中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,此问题的标题已更改为更专注于确切问题的标题.请参阅评论流,了解我是如何意识到问题是AngularJS似乎无法处理{{isNaN()}}

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() }}

我的HTML中包含以下内容:

I have the following in my HTML:

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

我也尝试过:

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

和:

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

更新1 我尝试了以下操作:

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

它给出了这个:

aa false bb false cc

更新2 我不确定这是否有帮助,但这是填充option.selectedSubject值的内容.在这种情况下,本地存储中什么也没有:

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'));

当我检查$ scope.option.selectedSubject的值时,它是 NaN

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

推荐答案

这里的问题不是isNaN.问题是您正在$ scope变量上调用一个不存在的函数.

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

每个内插的文本('{{text}}')均与$ scope变量相关联,并针对该$ scope变量进行评估.

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

为方便起见,可以动态创建$ scope.property(如果不存在).每当您使用ngModel ='someProperty'或{{aPropertyName}}时,就会自动为您创建相应的$ scope.someProperty或$ scope.aPropertyName(如果尚不存在).但是,此快捷方式仅适用于基元.

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.

函数调用始终针对$ scope进行评估,并且从未创建.这就是为什么您必须说$ scope.isNaN = isNaN的原因,如您先前的评论中所述.

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.

尝试使用任何功能. isArray,isNumber等.除非您将$ scope.functionName = functionName放在控制器中的某个位置,否则它将无法工作.

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

此外,如果您真的想在插值中正确执行isNaN测试,则可以利用javascript的类型系统并使用

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

这篇关于为什么{{isNan(x)}}在angularJS中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆