Alex Rivera | Logout

Knockout.js - javascript function on data-bind

Asked 2012-03-02T16:32:15.380
25

Is there a way i can call JavaScript function on data-bind like this:

<span id="lblSomePropVal" data-bind="text: MySomeFunction(SomeProperty())" ></span>

What i am trying to do is call MySomeFunction with the value of SomeProperty of my viewmodel. My SomeFunction will return some text based on value passed and that will be displayed in the span lblSomePropVal.

I tried it the way i have written in the example but it throws binding error.

Am i missing something here or is there any other way of doing this?

This is the error i am getting:

Microsoft JScript runtime error: Unable to parse bindings.
Message: [object Error];
Bindings value: text: MySomeFunction(SomeProperty())
Edit
Report

1 Answer

18

I had a similar problem trying to calculate table cell entries. What worked for me was including 'MySomeFunction' in my data model, and then data-binding my table cells as:

<td data-bind="text: $root.MySomeFunction(SomeProperty)"></td>
answered 2012-09-09T15:11:49.760

Your Answer