Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add static text on Knockout.js data-bind

Tags:

knockout.js

Is it possible to add static text to data-bind with something like the example below:

<p data-bind="text:someProperty"></p>

I would like to add static text as below:

<p data-bind="text:' + $' + someProperty"></p>
like image 813
Nil Pun Avatar asked May 23 '12 06:05

Nil Pun


2 Answers

I'm quite late, (and new to Knockout) but here is another option I have used to add static text around an observable. In my case I wanted to add parentheses around a value that was chosen from a drop-down.

javascript

...
return function AppViewModel() {
    this.myOptionValues = ko.observableArray([
        { label: "Foo", value: "Bar" },
    ]);

    this.mySelectedValue = ko.observable();

    this.myValue = ko.computed(function () {
        return this.mySelectedValue() ? '(' + this.mySelectedValue().label + ')' : '';
    }, this);
};
...

html

<small data-bind="text: myValue"></small> // (Foo)
like image 198
Damon Avatar answered Sep 16 '22 14:09

Damon


Can you not put the static text inside the paragraph tag along with a span tag and data-bind on the span?

<p>text: <span data-bind="[whatever]" /></p>
like image 41
LDJ Avatar answered Sep 18 '22 14:09

LDJ