简体   繁体   English

简单的Knockout.JS数据绑定

[英]Simple Knockout.JS Data Binding

I just want to pass in a parameter to a function like this: 我只想将参数传递给这样的函数:

<button data-bind="click: myFunction( text: surveyId )"></button>

function myFunction(param) {

  alert(p);

}

So, I just want to pass in a parameter to a function that is part of my knockout model. 因此,我只想将参数传递给我的剔除模型的一部分的函数。 I want this function to reside outside of the knockout code. 我希望此函数驻留在剔除代码之外。

Very, very basic stuff here. 非常非常基本的东西。 Can anyone help me out? 谁能帮我吗?

If you need to pass a parameter in like this, you can use an anonymous function, like this: 如果您需要像这样传递参数,则可以使用匿名函数,例如:

<button data-bind="click: function(){ myFunction(surveyId); }"</button>

function myFunction(param) {
  alert(param);
}

If the view model property is an observable, you'll need to account for that - either by retrieving the value directly in the binding: 如果视图模型属性是可观察的,则需要考虑这一点-通过直接在绑定中获取值:

<button data-bind="click: function(){ myFunction(surveyId()); }"</button>

or unwrapping it as part of the external function: 或将其展开为外部功能的一部分:

function myFunction(param) {
  alert(ko.utils.unwrapObservable(param));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM