在不使用HTML表单标记

时间:2016-09-07 08:07:11

标签: angularjs validation

我想在不使用HTML表单的情况下应用Angular Form Validation。我试过跟随,但它没有禁用按钮。



<ng-form name="login">

 <div class="input-group">
   <span class="input-group-addon"><i class="fa fa-user"></i></span>
   <input type="text" name="username" value="" placeholder="Username" class="form-control" id="login-username" ng-required="true">
 </div>

 <div class="input-group">
   <span class="input-group-addon"><i class="fa fa-lock"></i></span>
   <input type="password" id="login-password" class="form-control" name="password" placeholder="Password" ng-required="true">
 </div>

 <button style="width: 100%;" title="" data-original-title="" role="button" class="btn custom-small-btn btn-primary hvr-float-shadow" ng-disabled="login.username.$invalid || login.password.$invalid">Login</button>

</ng-form>
&#13;
&#13;
&#13;

尝试使用ng-diabled="login.$invalid",但也没有用。我该怎么做才能使它发挥作用。

1 个答案:

答案 0 :(得分:1)

只需在表单字段中使用ng-model,它就可以工作::

 <ng-form name="login">

 <div class="input-group">
   <span class="input-group-addon"><i class="fa fa-user"></i></span>
   <input type="text" ng-model="username" name="username" value="" placeholder="Username" class="form-control" id="login-username" ng-required="true">
 </div>

 <div class="input-group">
   <span class="input-group-addon"><i class="fa fa-lock"></i></span>
   <input type="password" ng-model="password" id="login-password" class="form-control" name="password" placeholder="Password" ng-required="true">
 </div>

 <button style="width: 100%;" title="" data-original-title="" role="button" class="btn custom-small-btn btn-primary hvr-float-shadow" ng-disabled="login.username.$invalid || login.password.$invalid">Login</button>

</ng-form>