question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trouble setting ngModel for chosen dropdown programmatically.

See original GitHub issue

Hi, I’m trying to programmatically set the value for some dropdowns using chosen, but I’m struggling to get the dropdown to actually display the corresponding value. I have a dropdown that looks like this:

<select name="testitem" class="form-control" chosen allow-single-deselect="true" ng-model="item.itemID" ng-options="option.optionID as option.Text for option in options track by option.optionID">
    <option value=""></option>
</select>

And my ngOptions data looks like this:

$scope.options = [
   {optionID: 101, Text: 'Text to display in options'},
   {optionID: 102, Text: 'Text to display in options 2'},
   {optionID: 103, Text: 'Text to display in options 3'}
];

In my Angular controller for the view, I’m immediately setting the data for $scope.options and then $scope.item.itemID, but no matter what value I throw in that itemID, the chosen dropdown doesn’t update to reflect it. I’ve checked the markup and verified that a <select> element containing <option> elements with optionIDs for their values is being generated and the values I’m choosing line up. I’m very confused, because we have another project that uses (key, value) syntax in the ngOptions attribute where this behavior works as expected. Is there something special about using (key, value) in ngOptions that allows chosen to respect the two-way data binding, or is there something else that I’m missing?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cgatesmancommented, Oct 26, 2015

+1

I have found that this is broken only when using track by in the ng-options.

0reactions
leocaseirocommented, Mar 16, 2016

@swiftened as same way you would do with a normal <select>, the correct way is using a model with your ID only. You don’t need track by in that case, like so:

<select name="testitem"
    chosen
    ng-model="testitem" allow-single-deselect="true"
    ng-options="option.optionID as option.Text for option in options">
    <option value=""></option>
</select>
$scope.options = [
   {optionID: 101, Text: 'Text to display in options'},
   {optionID: 102, Text: 'Text to display in options 2'},
   {optionID: 103, Text: 'Text to display in options 3'}
];

  $scope.testitem = 103; 

An example working http://plnkr.co/edit/UH35hn?p=preview

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to programmatically change the selected option in an ...
on select all you have to do is to set ngModel attribute and then you can change variable value to set value ...
Read more >
ng-select How to programmatically set a value
It seems using # is the only way to get a instance of ng-select within the component's typescript.
Read more >
Clear item in Angular Drop down list component
Through programmatic you can set null value to anyone of the index, text or value property to clear the selected item in DropDownList....
Read more >
SelectControlValueAccessor
The ControlValueAccessor for writing select control values and listening to select control changes. The value accessor is used by the FormControlDirective ...
Read more >
Value Binding - DropDownList - Kendo UI for Angular
Use the ngModel value binding. If the value is set by the ngModel value binding, the framework will automatically update the corresponding field...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found