javascript - How to pick an indexed value from a knockout array -


I want to find an array available, with a pointer variable 'ID', this is snippet from my code:

  function items (id, category, name, selected) {var self = this; Self.id = ko.observable (ID); Self.Category = ko.observable (category); Self. Name = Co.obswable (name); Self. Selected = Ko.SourceVoice (selected); } ViewModel () {var itself = this; Self.availableItems = ko.observableArray (); Self.init = function () {self.availableItems.push (New item (1, "A", "Hello", Incorrect)); Self.availableItems.push (new item (2, "A", "hi", true)); Self.availableItems.push (new item (3, "A", "bye", wrong)); } Self.id = ko.observable (1); } Var viewModel = new ViewModel (); Ko.applyBindings (ViewModel); ViewModel.init ();  

How does the following HTML line work?

  indexed line: & lt; Span data-bind = "text: $ root.availableItems () [$ root.id ()] and selected ()" gt; & Lt; / Span & gt;  

Because you must start your visual model before bonding it.

This will work:

  var viewModel = new ViewModel (); ViewModel.init (); Ko.applyBindings (ViewModel);  

When you call, Knockout is trying to parse and apply your binding. But your array of achievements available till then is still empty, thus you are getting an exception.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -