@scotty_walker
To populate a drop-down list with a list of strings using Knockout.js, you can follow these steps:
1 2 3 4 5 |
var viewModel = {
stringList: ko.observableArray(['Option 1', 'Option 2', 'Option 3'])
};
ko.applyBindings(viewModel);
|
1 2 3 |
<select data-bind="foreach: stringList">
<option data-bind="text: $data"></option>
</select>
|
This code will create a drop-down list with <option> elements for each item in the stringList array.
1 2 3 |
<select data-bind="value: selectedString, foreach: stringList">
<option data-bind="text: $data"></option>
</select>
|
Make sure to add a corresponding observable in your view model to store the selected value:
1 2 3 4 5 6 |
var viewModel = {
stringList: ko.observableArray(['Option 1', 'Option 2', 'Option 3']),
selectedString: ko.observable()
};
ko.applyBindings(viewModel);
|
Now, the selectedString observable will be updated with the value of the selected option in the drop-down list.