Reputation: 70
I want to pass a data-bind value as function parameter.
Here is sample code:
<data-bind="click: function (data) { myFunction('param1', data) }">
this is my view model
var vm = {
Category: ko.observableArray(),
Product: ko.observableArray(),
CatID: ko.observable("113"),
save: saveChanges,
Filter: FilterProduct,
};
CatID is an observable so there are some functions who change the CatID when they are called. and i want to pass this CatID as a parameter when specific button clicked.
Please help how can i do this.
Upvotes: 2
Views: 3359
Reputation: 8053
You can pass the observable in the data-bind like this
<button data-bind="click: myFunction(CatID)">Test Click</button>
And define your function in your ViewModel:
var vm = {
Category: ko.observableArray(),
Product: ko.observableArray(),
CatID: ko.observable("113"),
myFunction: function (obs) {
alert(obs());
}
};
I have created an example here: http://jsfiddle.net/nyothecat/QruPg/
Upvotes: 5