简体   繁体   English

Angular指令中的多个“或”表达式

[英]Multiple “or” expressions in an Angular directive

I am trying to check to see if 1 of 4 conditions are being met on 2 input fields in order to enable a Save button using an AngularJS expression in the ng-disabled directive. 我试图检查是否在2个输入字段上满足4个条件中的1个,以便在ng-disabled指令中使用AngularJS表达式启用“保存”按钮。

The code below will enable the Save button only if the first two conditions are met. 以下代码仅在满足前两个条件的情况下才会启用“保存”按钮。 So if the user enters any text in the email input field thus making it $dirty and $valid , the Save button will become enabled. 因此,如果用户在电子邮件输入字段中输入任何文本,从而使其成为$dirty$valid ,则将启用“保存”按钮。 But if the user enters any text into the username input field, the Save button will remain disabled. 但是,如果用户在用户名输入字段中输入任何文本,则“保存”按钮将保持禁用状态。

How can I get the last two conditions to be seen? 我如何才能看到最后两个条件?

<form name="userForm" ng-submit="submitForm(userForm.$valid)" novalidate>
  <label>Username</label>
  <input type="text" name="username" ng-model="user.name" required>
  <label>Email</label>
  <input type="text" name="email" ng-model="user.email" required>
  <button type="submit" ng-disabled="userForm.email.$pristine || userForm.email.$invalid || userForm.username.$pristine || userForm.username.$invalid">Save</button>
</form>

UPDATE 1 更新1

I ended up adding 2 more conditions. 我最终增加了2个条件。 It is almost working perfectly now. 现在,它几乎可以正常运行。

When the page loads the Save button is disabled by default because both fields are $prisitine . 页面加载时,默认情况下禁用“保存”按钮,因为两个字段均为$prisitine If I delete or add a character to either the username or email field, the Save button will get activated because the field gets $dirty . 如果我在用户名或电子邮件字段中删除字符或将其添加到字符中,则保存按钮将被激活,因为该字段获取$dirty So that works. 这样行得通。

The problem starts to happen if I first edit the username. 如果我首先编辑用户名,问题就会开始发生。 If I create a new valid username and then start editing the email field, I will hit points in the editing process where the email is not valid eg email or email@ or email@email. 如果我创建一个新的有效用户名,然后开始编辑电子邮件字段,我将在电子邮件无效的编辑过程中找到要点, 例如, emailemail@email@email. and at these points we expect the Save button to become disabled but it does not. 在这些时候,我们希望“保存”按钮被禁用,但实际上并没有。 Somehow once the username is $valid it ignores the fact that the email is $invalid . 一旦用户名是$valid它将以某种方式忽略电子邮件为$invalid的事实。

It does not stop there. 它不止于此。 If I load the page and just start deleting the preloaded email address char by char and hit all the $invalid positions, I will see the Save button successfully get disabled at each $invalid position. 如果我加载页面并开始按字符删除预加载的电子邮件地址char并点击所有$invalid位置,我将看到Save按钮成功在每个$invalid位置被禁用。 It is driving me crazy. 这让我发疯。

form(name="userForm", ng-submit="submitForm(userForm.$valid)", novalidate)

    div.form-group
        label Username
        input.form-control(type="text", name="username", ng-model="user.name", ng-minlength="3", ng-maxlength="23", required)
        div.error(ng-show="userForm.username.$invalid && userForm.username.$dirty")
            p.help-block(ng-show="userForm.username.$error.minlength") Username is too short.
            p.help-block(ng-show="userForm.username.$error.maxlength") Username is too long.
            p.help-block(ng-show="userForm.username.$error.required") Username is required.

    div.form-group
        label Email
        input.form-control(type="email", name="email", ng-model="user.email", required)
        div.error(ng-show="userForm.email.$invalid && userForm.email.$dirty")
            p.help-block(ng-show="userForm.email.$error.required") Email is required.

    button(ng-model="button", ng-disabled="(userForm.username.$error.minlength || userForm.username.$error.maxlength || userForm.username.$error.required || userForm.username.$pristine || userForm.username.$invalid) && (userForm.email.$error.required || userForm.email.$pristine || userForm.email.$invalid)") Save

UPDATE 2 更新2

I found a better way to enable the Save button. 我找到了启用“保存”按钮的更好方法。

<button type="submit" ng-disabled="userForm.$pristine || userForm.$invalid">

Works perfect now. 现在工作完美。

Maybe you could toggle the state of your submit button just by checking if the form is $invalid and inputs are $pristine . 也许只需检查form是否为$invalid且输入为$pristine即可切换提交按钮的状态。

So, your inputs would be 因此,您的输入将是

<input 
  type="text" 
  name="username" 
  placeholder="user name" 
  ng-model="user.name" 
  ng-minlength="3" 
  ng-maxlength="23"
  required></input>

<input 
  type="email" 
  name="email" 
  placeholder="email" 
  ng-model="user.email" 
  required></input>

<button
  type="submit" 
  ng-disabled="(userForm.username.$pristine || userForm.email.$pristine) || userForm.$invalid">Save</button>

<-- ng-disabled="(userForm.$pristine || userForm.$invalid" -->

See related Plunker here http://plnkr.co/edit/7pkRAn "" 在此处查看相关的Plunker http://plnkr.co/edit/7pkRAn “”

我猜你想要什么,条件应该像这样:

ng-disabled="(userForm.email.$pristine || userForm.email.$invalid) && (userForm.username.$pristine || userForm.username.$invalid)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM