Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout binding on the anchor tag

Tags:

knockout.js

I have an anchor tag with an icon inside it like this:

<a>
    <i class="icon-flip-2"></i>
    hello
</a>

I would like to replace the hello text with an binded element. Something like:

<a data-bind="text: myValue">
    <i class="icon-flip-2"></i>
    hello
</a>

The problem is that I lost the element < i class="icon-flip-2">

I want to keep it and be able to bind something on the anchor tag.

Thanks.

Any idea?

like image 356
Bronzato Avatar asked Mar 22 '13 13:03

Bronzato


People also ask

What are Knockout bindings?

Knockout provides built-in bindings to enable and disable input elements. The enable binding will enable the input element if the property it's bound to evaluates to true, and will disable the element if it evaluates to false.

What is a Knockout observable?

Knockout. js defines an important role when we want to detect and respond to changes on one object, we uses the observable. An observable is useful in various scenarios where we are displaying or editing multiple values and require repeated sections of the UI to appear and disappear as items are inserted and deleted.

What is two way binding in Knockout JS?

KO is able to create a two-way binding if you use value to link a form element to an Observable property, so that the changes between them are exchanged among them. If you refer a simple property on ViewModel, KO will set the form element's initial state to property value.


1 Answers

Use a virtual element

<a>
    <i class="icon-flip-2"></i>
    <!-- ko text: myValue --><!-- /ko -->
</a>

or a <span>

<a>
    <i class="icon-flip-2"></i>
    <span data-bind="text: myValue, if: myValue().length > 0"></span>
</a>
like image 188
Tomalak Avatar answered Sep 17 '22 18:09

Tomalak