KnockOutJs:为什么单击数据绑定已在元素加载时执行? [英] KnockOutJs: Why does click data-bind has execute on-load of element?

查看:85
本文介绍了KnockOutJs:为什么单击数据绑定已在元素加载时执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过php生成的锚链接,它将在ko上绑定并且可以正常工作.我的问题是为什么ko函数在元素加载时执行?下面是生成的代码.

I have a anchor link generated via php which will be binded on ko and works fine. My problem is why does the ko function is executed on load of the elements? below is the code generated.

html:

<a data-bind="click: addOrderedProducts(11,"CRM130930001","Cream",0.00,0,0,0)" class="Add" title="Add" href="">Add</a>

ko功能:

self.addOrderedProducts = function (id,product_number,name,price,quantity,discount,balance){
    self.orderedProducts.push(new Product(id,product_number,name,price,quantity,discount,balance));
};

请帮助我...预先感谢:)

please help me... Thanks in advance :)

推荐答案

这是对象文字在Javascript中的工作方式,因此在创建对象时立即评估诸如函数类之类的属性值.

This is how object literals are working in Javascript so the property values like function class immediately evaluated when the object gets created.

要使其正常工作,您需要将click绑定中的函数调用包装到匿名函数中:

To make it work you need to wrap your function call in the click binding into an anonymous function:

<a data-bind="click: function () { addOrderedProducts( ... ) }" href="">Add</a>

另请参见文档:访问事件对象或传递更多参数

这篇关于KnockOutJs:为什么单击数据绑定已在元素加载时执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆