当$渲染()的ngModel真实的数据叫什么名字? [英] When is $render() of ngModel called with real data?
问题描述
我写一个指令与ngModel合作。如示例所示,我设置$渲染控制器我在函数功能。
I am writing a directive to cooperate with ngModel. As shown in the example, I set the $render function on the controller to my function.
当的code初始化时,它与该模型的实际值调用两次,第一次用$ modelValue和$ viewValue集为NaN,然后在第二时间
When the code initializes, it is invoked twice, the first time with $modelValue and $viewValue set to NaN, and then a second time with the actual value of the model.
问题是,NaN的是测试一个熊。功能 isNaN()
是毫无价值的,到目前为止,我所看到的(它返回假()
,但如此 [。]
)和 Number.isNaN()
未得到广泛的支持。
The problem is, NaN is a bear to test for. The function isNaN()
is worthless, so far as I can see (it returns false for [""]
but true for ["."]
) and Number.isNaN()
is not widely supported.
有什么建议?
推荐答案
的 $ modelValue
和 $ viewValue
的解决 ngModel
永远只能承担(除非特别指定) NaN的
的值在最开始的 - 在链接时 - 和之前的任何 $格式化
, $渲染
和 $验证
(按顺序)有机会运行。
The $modelValue
and $viewValue
of ngModel
only ever assume (unless specifically assigned) the value of NaN
in the very beginning - at link-time - and before any $formatters
, $render
and $validators
(in that order) had a chance to run.
在换句话说,如果你要记录在不同的点这些价值,你会得到以下(假定 ngModel
变量设置为富
)
In other words, if you were to log at various points these values, you'd get the following (assuming the ngModel
variable is set to "foo"
):
link-time $formatters $render $validators
-----------------------------------------------------
$modelValue NaN "foo" "foo" "foo"
$viewValue NaN NaN "foo" "foo"
在的话,除非你需要访问 ngModel
在链接
- 时间,有没有需要防范 NaN的
。此外,也没有双调用 - 在 ngModel
管每变化一次运行
In words, unless you need to access ngModel
at link
-time, there is no need to guard against NaN
. Moreover, there is also no double invocation - the ngModel
pipe runs once per change.
NaN的在 $渲染
函数,因为你手动调用 $渲染
在链接时
Given the example you cite in comments my guess that you are seeing NaN
in the $render
function because you manually invoke the $render
at link-time.
这篇关于当$渲染()的ngModel真实的数据叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!