1

In knockout, this works and binds correctly:

self.currentCustomer = ko.observable(new Customer("a","b","c","d","e","f","g","h"));

However, the below does not.

// Random list of customers
self.customers = ko.observableArray([
    new Customer("a","b","c","d","e","f","g","h")
]);


self.currentCustomer = ko.observable(self.customers[0]);

I cannot figure out why this is not working. This pattern in working correctly in other parts of my application.

2 Answers 2

3

To access the array, you must unwrap it:

self.currentCustomer = ko.observable(self.customers()[0]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I knew it had to be something simple.
0

Just to add to that, the View is as below:

<p>Customer Name <input data-bind="value: currentCustomer().name" /></p>

The model is as below:

function AppViewModel() {
   // Random list of customers
self.customers = ko.observableArray([
    new Customer("ABC Corp.")
]);


self.currentCustomer = ko.observable(self.customers()[0]); // This is the correction.

}


function Customer(_name)
{
    this.name = ko.observable(_name);
}

// Activates knockout.js
ko.applyBindings(new AppViewModel());

Comments

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.