如何在Aurelia中设置复选框绑定 [英] How to set up a checkbox binding in Aurelia
问题描述
我有一个复选框列表,当用户选中一个复选框时,在.js文件中调用了一个函数,然后依次调用了dataservice.js方法(该方法调用了webapi控制器),这一切正常,并返回正确的数据.
I have a checkbox list, when the user checks one of the checkbox a function is called in the .js file and it in turn calls a method dataservice.js which calls a webapi controller, this all works fine and returns the correct data.
该过程完成后会发生的事情是未选中触发该序列的复选框.我已经检查了结果和schoolDistrict.该项目的IsChecked设置为true,这是正确的.
What happens when the process is finished is that the checkbox that fired the sequence isn't checked. I've inspected the result and schoolDistrict.IsChecked for that item is set to true, which is correct.
如何获取复选框?
下面是代码,但是我不确定check.one-bind绑定
Below is the code, but I am unsure about the check.one-way bind
<li repeat.for="schoolDistrict of schools.Districts">
<input type="checkbox" checked.one-way="schoolDistrict.IsChecked" value="${schoolDistrict.Value}" click.trigger="searchSchoolDistrict()"/>${schoolDistrict.Name}
</li>
任何帮助将不胜感激.
推荐答案
这里有一些问题:
- 问题可能出在您的
searchSchoolDistrict()
代码正在更改IsChecked
属性,但是one-way
绑定没有监听更改. - 虽然可以插值,但使用绑定语法可能是更好的样式.
- 设置
change.delegate
更加可靠,并且可以侦听复选框上的所有更改,这是复选框的最佳做法. - 已弃用,请确保为
searchSchoolDistrict()
选择了适当的范围,因为它可能位于$parent
而不是schoolDistrict
上.
- The problem is probably that your
searchSchoolDistrict()
code is changing theIsChecked
property, but theone-way
binding isn't listening for the change. - While interpolating the value would work, using the binding syntax is probably better style.
- Setting up a
change.delegate
is more robust, and will listen to all changes on the checkbox, which is a best practice for checkboxes. - Deprecated Make sure you select the proper scope for
searchSchoolDistrict()
, as it probably lives on the$parent
and notschoolDistrict
.
尝试使用此代替:
<li repeat.for="schoolDistrict of schools.Districts">
<input type="checkbox"
checked.bind="schoolDistrict.IsChecked"
value.one-way="schoolDistrict.Value"
change.delegate="searchSchoolDistrict()"/>
${schoolDistrict.Name}
</li>
这篇关于如何在Aurelia中设置复选框绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!