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

查看:22
本文介绍了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天全站免登陆