订阅数组 ko 中 vm 的属性
我是淘汰赛js的新手,并获得了一个可观察的数组,在填充数组时,我订阅了我添加的每个实例的属性,当属性更改时(由于用户交互),我需要知道哪个对象发生了变化,但是淘汰赛只给我相关财产的新价值。 可以拿到对象吗? (我在函数上下文中尝试了“this”但没有成功)
length.isSelected.subscribe(function (isSelected) {
if (isSelected) { // no access to actual object only the isSelected value
debugger;
spotLenghts.push(this);
} else {
spotLenghts.pop(this);
}
});
I'm new to knockout js and got an observable array, when populating the array I subscribe on a property of each instance I'm adding, when the property changed (due to user interaction) I need to know which object changed, but knockout give me only the new value of the relevant property.
Is it possible to get the object? (I tried "this" in the function context without success)
length.isSelected.subscribe(function (isSelected) {
if (isSelected) { // no access to actual object only the isSelected value
debugger;
spotLenghts.push(this);
} else {
spotLenghts.pop(this);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
subscribe
函数采用第二个参数,即target
。它将控制执行函数时this
设置的内容。因此,您可以将
length
(或您根据需要创建的更高级别的对象)作为第二个参数传递,并能够在处理程序中使用this
。The
subscribe
function takes a second argument that is thetarget
. It will control whatthis
will be set to when your function is executed.So, you can potentially pass
length
(or a higher level object that you are creating as appropriate) as the second argument and be able to usethis
in your handler.