1

how do I bind a ko.observablsArray inside an array? I have the following viewModel:

function GetBegriffsKetteViewModel () {
    var self = this;
    self.Begriffsketten = ko.observableArray();
    self.letzterBegriff = ko.observable(0);
    self.aktuelleKette = -1;
    }

var Begriffskette = function () {
    var self = this;
    var begriffe = new ko.observableArray();
    var begriffBez = new ko.observableArray();
}

And in my html I have the following div:

<div id="ausgewaehlteKetten" data-bind="foreach : Begriffsketten">
    <div class="row">
        <div class="col-md-2" id="innerDivForEach" data-bind="foreach : begriffBez">
            <span data-bind="text: $data" ></span>
        </div>
    </div>
</div>

If I run this code I get the following error:

ReferenceError: begriffBez is not defined

Is there a way to bind an array inside an array? What am I doing wrong?

Thanks

Thomas

3
  • Sorry I just saw that one line in my html example is missing Commented Oct 17, 2016 at 14:51
  • <div id="ausgewaehlteKetten" data-bind="foreach : Begriffsketten"> <div class="row"> <div class="col-md-2" id="innerDivForEach" data-bind="foreach : begriffBez"> <span data-bind="text: $data" ></span> </div> </div> </div> Commented Oct 17, 2016 at 14:51
  • Added the new HTML to your question. Commented Oct 17, 2016 at 14:54

1 Answer 1

3

First, you should not create knockout objects with new.

  • wrong: var x = new ko.observable();
  • correct: var x = ko.observable();

Second, if you want a variable to become an object property, you must assign it to the object instance (this - or, in your case, self).

Minor point: By convention, only constructors should start with a captital letter in JS. So since it's a regular property it would be begriffsketten with a lowercase b.

function BegriffsKetteList() {
    var self = this;
    self.begriffsketten = ko.observableArray();
    self.letzterBegriff = ko.observable(0);
    self.aktuelleKette = ko.observable();  // better use an observable here
}

function Begriffskette() {
    var self = this;
    self.begriffe = ko.observableArray();
    self.begriffBez = ko.observableArray();
}

Once you do that, knockout can see the begriffBez and begriffe properties when it renders the view.

<div id="ausgewaehlteKetten" data-bind="foreach: begriffsketten">
    <div class="row">
        <div class="col-md-2" id="innerDivForEach" data-bind="foreach: begriffBez">
            <span data-bind="text: $data" ></span>
        </div>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot. I was the whole day trying to make it work but was allways looking at the html.
Now it works, but if I add a new element to the array, The view is not refreshed with the new item. Is there a way to archive this,too? I thougth the observalbe would do this.
Yes, it does. It depends on how you add the new element. There are questions about this, try to search. The official documentation also shows how to add items to an observable array. Try to find out on your own first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.