验证ng-form Angular不起作用

时间:2017-03-14 04:40:42

标签: angularjs forms validation

我使用ng-form创建一个表单并想要在输入字段无效时禁用该按钮,我已经尝试过但它仍然无效。我不是我所缺少的。

这是代码

 <button 
        class="btn btn-primary btn-sm" 
        ng-click="compose('Submit',1)"
        ng-disabled="memo.nestedForm.perihal.$invalid">
        <i class="fa fa-paper-plane-o">&nbsp;</i> 
        Kirim
    </button>
<form class="tab-form-demo" ng-controller="userCtrl" role="form" name="memo" novalidate>
    <uib-tabset active="activeForm">
      <uib-tab index="0" heading="Informasi Surat">
        <ng-form name="nestedForm" class="form-horizontal">
          <div class="form-group">
            <label class="col-md-1 col-sm-2 control-label">Perihal</label>
            <div class="col-md-10 col-sm-10">
                <input type="text" class="form-control" ng-model="surat.subject" name="perihal" required>
                <div ng-show='nestedForm.perihal.$dirty && nestedForm.perihal.$invalid'>
                    <span ng-show='nestedForm.perihal.$error.required'>Required</span>
                </div>
            </div>
          </div>

        </ng-form>
      </uib-tab>

    </uib-tabset>
  </form>

2 个答案:

答案 0 :(得分:0)

尝试从标签内插入按钮。

<form class="tab-form-demo" ng-controller="userCtrl" role="form" name="memo" novalidate>
    <uib-tabset active="activeForm">
      <uib-tab index="0" heading="Informasi Surat">
        <ng-form name="nestedForm" class="form-horizontal">
          <div class="form-group">
            <label class="col-md-1 col-sm-2 control-label">Perihal</label>
            <div class="col-md-10 col-sm-10">
                <input type="text" class="form-control" ng-model="surat.subject" name="perihal" required>
                <div ng-show='nestedForm.perihal.$dirty && nestedForm.perihal.$invalid'>
                    <span ng-show='nestedForm.perihal.$error.required'>Required</span>
                </div>
            </div>
          </div>

        </ng-form>
      </uib-tab>

    </uib-tabset>

       <button 
        class="btn btn-primary btn-sm" 
        ng-click="compose('Submit',1)"
        ng-disabled="memo.nestedForm.perihal.$invalid">
        <i class="fa fa-paper-plane-o">&nbsp;</i> 
        Kirim
    </button>
  </form>

答案 1 :(得分:0)

您应将表单内的按钮移至validate

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
<body>

<h2>Validation Example</h2>

<form ng-app="myApp" ng-controller="validateCtrl" 
name="myForm" novalidate>

<p>Username:<br>
<input type="text" name="user" ng-model="user" required>
<span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid">
<span ng-show="myForm.user.$error.required">Username is required.</span>
</span>
</p>

<p>Email:<br>
<input type="email" name="email" ng-model="email" required>
<span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
<span ng-show="myForm.email.$error.required">Email is required.</span>
<span ng-show="myForm.email.$error.email">Invalid email address.</span>
</span>
</p>

<button 
        class="btn btn-primary btn-sm" 
        ng-click="compose('Submit',1)"
        ng-disabled="myForm.user.$dirty && myForm.user.$invalid ||  
myForm.email.$dirty && myForm.email.$invalid">
        <i class="fa fa-paper-plane-o">&nbsp;</i> 
        Kirim
    </button>


</form>

<button 
        class="btn btn-primary btn-sm" 
        ng-click="compose('Submit',1)"
        ng-disabled="myForm.user.$dirty && myForm.user.$invalid ||  
myForm.email.$dirty && myForm.email.$invalid">
        <i class="fa fa-paper-plane-o">&nbsp;</i> 
        Kirim outside form
    </button>




<script>
var app = angular.module('myApp', []);
app.controller('validateCtrl', function($scope) {
    $scope.user = 'Test';
    $scope.email = 'Test@gmail.com';
});
</script>

</body>
</html>

请运行以上代码,然后选中两个按钮

HERE is the Working DEMO