JavaScript 数组是 NaN?AngularJS ngModel [英] JavaScript array is NaN? AngularJS ngModel

查看:24
本文介绍了JavaScript 数组是 NaN?AngularJS ngModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看 ngModel 的价值是什么:

I'm trying to see what the value of an ngModel is:

.directive('myDir', function() {
  return {
    require: '?ngModel',
    link: function(scope, elm, attr, ngModel) {
      if (!ngModel)
        return

      console.log(ngModel)
      console.log(ngModel.$modelValue)
    }
  };
})

即使我的 ngModel 是一个数组,它也会记录 NaN?

Even though my ngModel is an array it logs NaN?

推荐答案

$viewValue$modelValue默认为 Number.NaN -- Not - a - Number 的 JavaScript 定义.

$viewValue and $modelValue default to Number.NaN -- JavaScript Definition for Not - a - Number.

检查 Github 然后你会发现

check Github and you find that

var NgModelController = ['$scope', '$exceptionHandler', '$attrs', 
                                 '$element', '$parse',
'$animate', '$timeout',
    function($scope, $exceptionHandler, $attr, $element, $parse, 
                   $animate, $timeout) 
  {
  this.$viewValue = Number.NaN;
  this.$modelValue = Number.NaN;

为什么这样方便?因为 AngularJS 试图避免出现像 nullundefined 这样的情况.视图值和模型值由范围"绑定和定义.这就是 $scope 服务的重点——管理 modelValue 和 viewValue.

Why is this convienient? Because AngularJS tries to avoid having cases like null and undefined. View Values and Model Values are bound and defined by "scope". That's the point of the $scope service -- to manage the modelValue and viewValue.

AngularJS 服务访问它们之前,它们默认为 number.NaN

这篇关于JavaScript 数组是 NaN?AngularJS ngModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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