Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.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 AngularJS表单验证中的ng类_Javascript_Angularjs_Forms - Fatal编程技术网

Javascript AngularJS表单验证中的ng类

Javascript AngularJS表单验证中的ng类,javascript,angularjs,forms,Javascript,Angularjs,Forms,以下关于angularJS表单验证的代码让我感到困惑。请参阅下面的HTML表单和JavaScript代码,这是我们的讲师给出的示例代码。我不明白的是这个标签 <div class="form-group" ng-class="{ 'has-error': feedbackForm.firstName.$error.required && !feedbackForm.firstName.$pristine }"> 可以使用“feedbackFrom.first

以下关于angularJS表单验证的代码让我感到困惑。请参阅下面的HTML表单和JavaScript代码,这是我们的讲师给出的示例代码。我不明白的是这个标签

    <div class="form-group" ng-class="{ 'has-error': feedbackForm.firstName.$error.required && !feedbackForm.firstName.$pristine }">
可以使用“feedbackFrom.firstName”访问它,因为您已经使用“feedbackFrom”命名了表单,并且输入的name属性的值为“firstName”

至于表单对象及其$error和$pristine,您可以在这里的文档中找到它:

您有点不对劲

feedbackForm.firstName
引用表单
feedbackForm
和输入字段
firstName

ngModel引用
$scope.feedback
对象并处理模型绑定


$error
$pristine
是AngularJS添加的类,用于指示输入字段的状态。

当您使用angular创建表单时,表单中的每个元素的引用都会在表单对象中创建

$error和$pristine由angular创建,表示表单字段的状态。如果firstName.$error=true,则firstName字段中输入的值不符合验证要求(例如长度)$pristine告诉angular是否触摸过形状。这可以防止在用户尚未输入任何信息时字段显示为无效

tl;博士:

$scope.feedback.firstName直接引用输入字段,而$scope.feedbackForm.firstName引用与表单相关的输入字段

<div ng-controller="FeedbackController">
    <form role="form" name="feedbackForm" ng-submit="sendFeedback()" novalidate>
      <div class="form-group" ng-class="{ 'has-error': feedbackForm.firstName.$error.required &&!feedbackForm.firstName.$pristine }">
       <label for="firstname" class="col-sm-2 control-label">First Name</label>
       <div  class="col-sm-10">
        <input type="text" class="form-control" id="firstname" name="firstname" placeholder="Enter First Name" ng model="feedback.firstName" required>
        <span ng-show="feedbackForm.firstName.$error.required && !feedbackForm.firstName.$pristine" class="help-block">Your first name is required.</span>
       </div>
      </div>
    </form>
    .controller('ContactController', ['$scope', function($scope) {

     $scope.feedback = {mychannel:"", firstName:"", lastName:"", agree:false, email:"" };

     var channels = [{value:"tel", label:"Tel."}, {value:"Email",label:"Email"}];

    $scope.channels = channels;
    $scope.invalidChannelSelection = false;

    }])

   .controller('FeedbackController', ['$scope', function($scope) {

    $scope.sendFeedback = function() {

    console.log($scope.feedback);

    if ($scope.feedback.agree && ($scope.feedback.mychannel == "")) {
       $scope.invalidChannelSelection = true;
       console.log('incorrect');
      }
    else {
       $scope.invalidChannelSelection = false;
       $scope.feedback = {mychannel:"", firstName:"", lastName:"", agree:false, email:"" };
       $scope.feedback.mychannel="";
       $scope.feedbackForm.$setPristine();
       console.log($scope.feedback);
       }
     };
  }])