简体   繁体   English

Aurelia中的disabled.bind无法正常工作

[英]disabled.bind in Aurelia not working correctly

I have a form, in the form there is a custom component that has certain fields in it. 我有一个表单,在表单中有一个包含某些字段的自定义组件。 At the end of the form there is a button. 表格末尾有一个按钮。 The button has... 该按钮有...

disabled.bind="!(formValid && subFromValid)"

Now, on the custom component I have a two-way binding of a variable "subFormValid" The subFormValid is only valid when the validation in the custom component is valid. 现在,在自定义组件上,我对变量“ subFormValid”进行了双向绑定。只有当自定义组件中的验证有效时,subFormValid才有效。 So, the sub form validates some fields and sets subFormValid = true. 因此,子表单会验证某些字段并设置subFormValid = true。 Even though the "formValid" is false, the button is now enabled. 即使“ formValid”为false,该按钮也已启用。 I can't figure out why and it is driving me nuts. 我不知道为什么,这让我发疯。 I even went so far as to add a get function to a variable and add console logs in it, like so... 我什至甚至为变量添加了get函数,并在其中添加了控制台日志,就像这样...

<button type="submit" disabled.bind="wholeFormValid">Submit</button>

Then in my class I have... 那我上课的时候

get wholeFormValid() {
  console.log("validating form");
  console.log(!(this.formValid && this.subFormValid));
  return !(formValid && subFormValid);
}

I get a million plus lines in the console, but I was able to watch it, the entire time. 我在控制台中获得了超过一百万行的信息,但是我一直都能观看。 When I first load the page it was logging... 当我第一次加载页面时,它正在记录...

validating form 验证表格

true 真正

Then I filled out the subform, and checked the console. 然后,我填写了子表单,并检查了控制台。 The console showed... 控制台显示...

validating form 验证表格

true 真正

Yet, the button was now enabled. 但是,该按钮现已启用。

For some reason whenever subFormValid = true, the button is enabled, regardless of formValid. 由于某种原因,无论何时subFormValid = true,都会启用该按钮,而与formValid无关。

Does anyone know how to disable a button unless 2 conditions are met? 除非满足两个条件,否则有人知道如何禁用按钮吗? Everything I do enables the button as soon as subFormValid is true, even though the console is still logging "true", which should disable the button. 我所做的所有操作都会在subFormValid为true时启用该按钮,即使控制台仍在记录“ true”,这也应该禁用该按钮。

Just to help out, if anyone is wondering why there is a subform in the form it is because the address needs to be validated using Smarty Streets and we want to be able to reuse that part of the form in other places, so I created a custom component for the address section that validates the input, and validates the address. 只是为了提供帮助,如果有人想知道为什么表单中存在子表单,这是因为需要使用Smarty Streets验证地址,并且我们希望能够在其他地方重用表单的那一部分,所以我创建了一个表单地址部分的自定义组件,用于验证输入并验证地址。 It is being called in the form like so... 它以这样的形式被调用...

<require from="components/smarty-streets"></require>

Then using like this... 然后像这样使用...

<smarty-streets form-is-validated.two-way="subFormValid"></smarty-streets>

Then in smarty streets I have... 然后在聪明的街道上,我...

@bindable formIsValidated;

and I change the value from true to false and vice-versa depending on the validation in the component. 然后我根据组件中的验证将值从true更改为false,反之亦然。

I have tried to recreate your problem using the following: 我尝试使用以下方法重新创建您的问题:

<input type="checkbox" checked.bind="formValid"/>
<input type="checkbox" checked.bind="subFormValid"/>
<button disabled.bind="wholeFormValid">Submit</button>

I noticed in your function that you used this.formValid in the console.log line, but in the return line you used formValid , without this . 我注意到在您的函数中,您在console.log行中使用了this.formValid ,但是在返回行中,您使用了formValid ,而没有this This seems to be a different variable than your actual binding variables. 这似乎是与实际绑定变量不同的变量。 I think your function should look like this: 我认为您的函数应如下所示:

get wholeFormValid() {
  console.log("validating form");
  console.log(!(this.formValid && this.subFormValid));
  return !(this.formValid && this.subFormValid);
}

Edit: I also strongly recommend using the @computedFrom decorator on your get() to reduce the amount of calculations aurelia does. 编辑:我也强烈建议在get()上使用@computedFrom装饰器,以减少@computedFrom的计算量。 You can read more on that here . 您可以在这里阅读更多内容。

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

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