简体   繁体   English

AngularJS禁用

[英]Angularjs ng-disabled

I have two inputs for validating email and password. 我有两个输入用于验证电子邮件和密码。 I can see individuals of the following works: 我可以看到以下作品的个人:

myForm.$error.required

validating input form with required input value and 验证具有所需输入值的输入表单,并

myForm.email.$valid

validating my regex email 验证我的正则表达式电子邮件

If i was to print ie {{ with the above code}} i can see the boolean result of the validation for both input and its correct. 如果我要打印,即{{使用上面的代码}},我可以看到输入及其正确的验证的布尔结果。 However when I use ng-disabled on button with both expression it doesnt work but if i put ng-disable with only one expression I can see it does work but I am unable to place both expression ie 但是,当我在两个表达式的按钮上都使用ng-disabled时,它不起作用,但是如果我仅使用一个表达式将ng-disable放在我的嘴里,我可以看到它的确起作用,但是我无法放置两个表达式,即

<button type="submit" ng-disabled="myForm.$error.required && !myForm.email.$valid">Sign in</button>

Currently the result becomes true based on the first expression but i thought && meant both condition needs to be met in order to result true ? 目前,根据第一个表达式,结果变为true,但我认为&&意味着必须同时满足两个条件才能得出true? Could it be because the ouput is true + false = true ? 可能是因为输出为true + false = true吗?

true + false doesn't mean true && false , it means true || false true + false并不表示true && false ,它表示true || false true || false , true || false

that's why you don't get the right behavior, so: 这就是为什么您无法获得正确行为的原因,因此:

<button type="submit" ng-disabled="myForm.$error.required || !myForm.email.$valid">Sign in</button>

You should use ng-disabled="myForm.$invalid" if you want to disable the button while the whole form is invalid. 如果要在整个表单无效时禁用按钮,则应使用ng-disabled =“ myForm。$ invalid”。

In case you want to disable the button if the email is not provided or email is not valid so you should use || 如果未提供电子邮件或电子邮件无效,您想禁用此按钮,则应使用||。 instead of && so the button will be disabled in both cases. 而不是&&,因此在两种情况下都将禁用该按钮。

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

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