禁用按钮,如果至少有一个输入字段使用ngDisabled是空的 [英] Disable a button if at least one input field is empty using ngDisabled

查看:151
本文介绍了禁用按钮,如果至少有一个输入字段使用ngDisabled是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想prevent从点击按钮注册,如果字段是不正确的用户。其中的一部分是,某些领域必须填写。但病情开始变得有点长:

I'd like to prevent the user from clicking the button "Register" if the fields are not correct. Part of that is that certain fields must be filled. But the condition is starting to get a bit long:

<button class="btn btn-success" ng-click="register()"
    ng-disabled="signInForm.$invalid || form.firstName.length==0 ||
    form.lastName.length==0 || form.email.length==0 ||
    form.password.length==0 || form.passwordBis.length==0">Register</button>

我如何可以简化?

How can I simplify that?

推荐答案

我觉得你需要的是你的窗体中的输入字段提供要求属性,所以直到该字段填写表格 signInForm 将是无效的。同样,你可以在输入提供其他验证属性为好。

I think what you need is to provide required attribute for your input fields inside your form, so until the fields are filled in the form signInForm will be invalid. Similarly you could provide other validation attributes on the inputs as well.

例如: -

  <input type="text" ng-model="firstName" name="firstName" required />
  <input type="text" ng-model="lastName" name="lastName" required />
  <input type="email" ng-model="email" name="email" required />
  ...
  ...
  <button class="btn btn-success" ng-click="register()" 
            ng-disabled="signInForm.$invalid">Register</button>

这篇关于禁用按钮,如果至少有一个输入字段使用ngDisabled是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆