S
S
stepanmalahov2014-05-16 15:32:19
JavaScript
stepanmalahov, 2014-05-16 15:32:19

How to get property value in template from associative array in knockout.js ?

Here is such a design:

Item = ko.observableArray();
//...
oneItem().item = item;
oneItem().category = category;

It returns the following contents of oneItem as a result:
oneItem = [
  category: 
    {
    checked: 1,
    name: "Вода",
    title: "Выбрать воду",
    type: "water",
    },
  item:
    {
    color: "Прозрачная",
    id: 2,
    name: "Просто вода",
    price: 100,
    src: "src/water.jpg",
    }
]

In template:
<img class="popup-img" data-bind="attr: { src: oneItem().item.src }"/>
<strong data-bind="text: oneItem().item.color"></strong>

As a result, I get the error Message: Cannot read property 'src' of undefined
But at the same time:
console.log(oneItem().item.src);
shows the required value!
How to access oneItem().item.src in this case?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question