Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.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 仅在单击“角度”菜单上的“保存”按钮后验证输入字段_Javascript_Html_Angularjs_Validation - Fatal编程技术网

Javascript 仅在单击“角度”菜单上的“保存”按钮后验证输入字段

Javascript 仅在单击“角度”菜单上的“保存”按钮后验证输入字段,javascript,html,angularjs,validation,Javascript,Html,Angularjs,Validation,我想验证我的输入必填字段,并仅在单击表单中的保存按钮(提交)后显示错误消息。 在angularjs中,最好的方法是什么 谢谢,请检查这个 您可以在该链接中找到已解释的示例 module = angular.module('app', []); module.directive('showErrors', function() { return { restrict: 'A', require: '^form', link: function (sco

我想验证我的输入必填字段,并仅在单击表单中的保存按钮(提交)后显示错误消息。 在angularjs中,最好的方法是什么

谢谢,

请检查这个

您可以在该链接中找到已解释的示例

module = angular.module('app', []);

module.directive('showErrors', function() {
    return {
      restrict: 'A',
      require: '^form',
      link: function (scope, el, attrs, formCtrl) {
        // find the text box element, which has the 'name' attribute
        var inputEl   = el[0].querySelector("[name]");
        // convert the native text box element to an angular element
        var inputNgEl = angular.element(inputEl);
        // get the name on the text box
        var inputName = inputNgEl.attr('name');

        // only apply the has-error class after the user leaves the text box
        inputNgEl.bind('blur', function() {
          el.toggleClass('has-error', formCtrl[inputName].$invalid);
        })
      }
    }
  });

module.controller('NewUserController', function($scope) {
  $scope.save = function() {
    if ($scope.userForm.$valid) {
      alert('User saved');
      $scope.reset();
    } else {
      alert("There are invalid fields");
    }
  };

  $scope.reset = function() {
    $scope.user = { name: '', email: '' };
  }
});

您尝试过的内容\请参见以下内容: