Angular没有给出复选框的当前状态 [英] Angular not giving current state of checkbox
问题描述
我已经开始构建ToDo示例应用程序.我面临一个奇怪的问题. 以下是待办事项列表的HTML代码.在该列表中,我将ngModel设置为复选框,并触发更改事件.
I have started building an ToDo sample app. I am facing an strange issue. Below is the HTML code for the ToDo List. In that list i am setting ngModel for checkbox and firing an change event as well.
代码以监听更改.我只是将当前状态打印到控制台.
Code to listen the change. I am only printing the current state to console.
看下面的图片,您可以看到该复选框的当前状态已选中,但是在控制台中,我正在获取该复选框的先前状态.
Look at below image you can see the current state of the checkbox is checked but in console i am getting the previous state of the checkbox.
请告诉我什么是使该方法正常工作的正确方法.
Please tell me what is the correct approach to have that working.
推荐答案
这是一个已知问题
这正在按预期方式工作.发出更改事件ngModelChange
([(ngModel)]
的(...)
部分尚未更新绑定模型:
This is working as intended. When the change event is emitted ngModelChange
(the (...)
part of [(ngModel)]
hasn't updated the bound model yet:
另请参见
- https://github.com/angular/angular/issues/3406,
- https://github.com/angular/angular/issues/6311
有不同的解决方法,例如使用event.target.value
而不是模型中的值.
There are different workarounds like using event.target.value
instead of the value from the model.
这篇关于Angular没有给出复选框的当前状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!