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.

When using ng-options on a single select after selecting an option attempting to select the last option will throw this error: "Uncaught TypeError: Cannot set property 'selected' of undefined"

See original GitHub issue

It’s an off by 1 error. ng-options is generating the list with n+1 options(the +1 being an empty option). The solution is to watch the select.length and on change throw an updated call.

Here is the fix, jam it in the linker function:

scope.$watch(function(){
    return element[0].length; 
}, function(newvalue, oldvalue){
    if (newvalue !== oldvalue) {
        element.trigger("chosen:updated");
    }
});

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:1
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
leocaseirocommented, Sep 12, 2016

Hi @climbican, try add an empty <option></option>. It should work as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AngularJS ng-options selecting default value after selecting ...
vm.modelObject.selectedOption will pass an isDefined check -- because it's defined, but with no value assignment.
Read more >
ngOptions - AngularJS: API
When an item in the <select> menu is selected, the array element or object property represented by the selected option will be bound...
Read more >
Fix Cannot Set Property of Null Error in JavaScript - YouTube
Check us out at https://www.skillforge.com The " cannot set property of null " error is a very common JavaScript issue that you may...
Read more >
A brief walk-through of the ng-options in AngularJS
The AngularJS documentation for 'select' says to use ng-options instead of ng-repeat in this scenario. The reason behind is, option element ...
Read more >
How to Fix 'cannot set properties of null' Errors in JS - Webtips
Lear what is the root cause and how you can fix 'Uncaught TypeError: Cannot set properties of null (setting 'value')' errors in JavaScript....
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