Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ng required不适用于;“南”;按单据计价_Javascript_Angularjs - Fatal编程技术网

Javascript ng required不适用于;“南”;按单据计价

Javascript ng required不适用于;“南”;按单据计价,javascript,angularjs,Javascript,Angularjs,我已经检查了解决方案,但没有得到解释。我怀疑这是一只虫子。(我可能错了,但我无法解释为什么它不起作用。) 当范围变量的值为NaN时,“ng required”在文档中无法正常工作。i、 e.应为该输入设置“必需”错误 这是plunker链接。 文档链接: $isEmpty HTML 我的问题是,, 1.为什么它不能像文档中解释的那样工作, 默认的$isEmpty函数检查值是否为未定义、、、null或NaN。 2.通过“ng submit”提交表单时为何有效。执行ng submit函数后显示错误

我已经检查了解决方案,但没有得到解释。我怀疑这是一只虫子。(我可能错了,但我无法解释为什么它不起作用。)

当范围变量的值为NaN时,“ng required”在文档中无法正常工作。i、 e.应为该输入设置“必需”错误

这是plunker链接。

文档链接: $isEmpty

HTML

我的问题是,, 1.为什么它不能像文档中解释的那样工作, 默认的$isEmpty函数检查值是否为未定义、、、null或NaN。 2.通过“ng submit”提交表单时为何有效。执行ng submit函数后显示错误


我希望普朗克能解释这个问题

实际发生的情况是,默认类型的输入[“text”]需要的ng检查$viewValue长度是否为0, 所以对于NaN,该值不是0。(这是什么?我测试了,结果是一个head)而对于null和undefined,条件是length==0是false

提交时,NaN实际上会清除并变为null,因此会显示“必需”

正如您在ng required的文档中所看到的:

我实际上想测试它,所以我创建了一个指令,记录测试“is length==0”,我得到了前两个异常和最后一个。。。错!:)(当我记录$viewValue时,我得到了“未定义”的结果,这很有趣)


你不需要将你的
输入
字段封装在
md content
标签中吗?你有什么理由不接受我的答案吗?
 <div data-ng-controller="demoController">
  <h3>Ng Required Does not work for NaN Value </h3>
  <h5>Documentation : https://docs.angularjs.org/api/ng/type/ngModel.NgModelController#$isEmpty</h5>
  <p style="color:blue;">According to docs =>  The default $isEmpty function checks whether the value is undefined, '', null or NaN.</p>

  <form onsubmit="javascript:return false;" novalidate id="testForm" name="testForm" data-ng-submit="submitTestForm()">
    <p>Undefined Value Test for "ng Requried"</p>
    <input id="undefinedValue" name="undefinedValue" data-ng-model="undefinedValue" data-ng-required="true"/>
    <span data-ng-show="testForm.undefinedValue.$error.required" style="color:red">Required</span>

    <p>Null Value Test for "ng Requried"</p>
    <input id="nullValue" name="nullValue" data-ng-model="nullValue" data-ng-required="true"/>
    <span data-ng-show="testForm.nullValue.$error.required" style="color:red">Required</span>

    <p>NaN Value Test for "ng Requried"</p>
    <input id="nanValue" name="nanValue" data-ng-model="nanValue" data-ng-required="true"/>
    <span data-ng-show="testForm.nanValue.$error.required" style="color:red">Required</span>


    <p></p><button type="submit">Test With Ng Submit </button></p>
    <p><button data-ng-click="submitTestForm()" type="button">Test Without Ng Submit</button></p>
  </form>

</div>
angular
  .module('demo',[])
  .controller('demoController',['$scope', function($scope){

      // Initial Data
      $scope.undefinedValue = undefined;
      $scope.nullValue = null;
      $scope.nanValue = NaN;

      // Submit test form handler
      $scope.submitTestForm = function(){

        if($scope.testForm.$valid !== true)
        {
          return false;
        }

      };
  }]);
.directive('testValue', function() {
  return {
      restrict: 'A',
      template: '',
      scope: {},
      require: 'ngModel',
      link: function(scope, iElement, iAttrs, ngModelCtrl) {
       scope.$watch("nanValue", function(){
          console.log(ngModelCtrl.$modelValue.length===0) ;
       });
      }
  };
});