Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KnockoutJS data-bind attr to a function doesn't seem to work

Tags:

knockout.js

I'm trying to data-bind an attribute with KnockoutJS to a function, so I can do some string manipulation, and struggling to get it to work.

The following works perfectly fine:

<div data-bind="html: doSomething($data)></div>

However, the following does not:

<a data-bind="attr: { src: doSomething($data)}">link</a>

Could somebody please help me get this working?

like image 385
JustinN Avatar asked Jul 12 '26 09:07

JustinN


1 Answers

There are possibly a couple of problems that you might be facing.

First, you should use href instead of src for an anchor's link.

Second, where does doSomething live? Is it a global function? Does it live on each item in an array or on the top-level view model. If it lives on the root view model, then you can refer to it using $root.doSomething from within the scope of an item that you are doing a foreach on.

Here is a sample: http://jsfiddle.net/rniemeyer/WbxPm/

like image 75
RP Niemeyer Avatar answered Jul 20 '26 08:07

RP Niemeyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!