robybas
robybas

Reputation: 3

Knockout select field error databind

I would to create a drop down list using Knockout. This is my select:

I don't understand why the "value" of my select field works only when I change the selection and do not work on the first time loading. I would the direct positioning on the element of 'Misure' (ObservableArray in the options) and I have always white space. The code is the follow:

self.MisuraLaboratorio = ko.observable();

self.MisuraLaboratorio = function () { 
    if (self.Indicazione() != null) { 
        for (i = 0; i < self.Misure().length; i++) { 
            if (self.Indicazione().Misura.Id == self.Misure()[i].Id) { 
                var data = self.Misure()[i]; 
                return data; 
                } 
            } 
        } 
    return " "; 
};

Upvotes: 0

Views: 105

Answers (1)

RP Niemeyer
RP Niemeyer

Reputation: 114792

When you to set an observable, you want to call it as a function with the value like:

self.MisuraLaboratorio(yourValue);

It looks like you are overwriting it with a function that finds your value.

Upvotes: 3

Related Questions