简体   繁体   中英

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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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