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.

ng-option with null value causes empty selection

See original GitHub issue

Describe the bug I have a use case where I need to add an option with null value. When the model’s value is null, the selection appears empty.

To Reproduce

<ng-select [(ngModel)]="dimensionalFilters.campaign" (ngModelChange)="generateReport()" 
       [clearable]="false">
       <ng-option [value]="null">Show All</ng-option>
       <ng-option *ngFor="let c of campaigns" [value]="c.id">{{ c.name }}</ng-option>
</ng-select>

Also, when selecting this option, the model is populated with {$ngOptionValue: null, $ngOptionLabel: "Show All", disabled: false}.

Expected behavior Show All should show up as selected and model value should be null.

Screenshots empty-selection

Additional context Using a default select this works as expected. Option is selected and model’s value is set to null when selecting this option.

<select [(ngModel)]="dimensionalFilters.campaign" (ngModelChange)="generateReport()">
    <option [ngValue]="null">Show All</option>
    <option *ngFor="let c of campaigns" [ngValue]="c.id">{{ c.name }}</option>
</select>

default-selection

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:34
  • Comments:20 (2 by maintainers)

github_iconTop GitHub Comments

14reactions
xxxKOTxxxcommented, Nov 23, 2019

If we add this change it could break a lot of functionality since we treat both null and undefined as empty or cleared state.

You treat wrong! Usage of -1/0/“i’m bad programmer”/etc. instead of null is the worst crutch I ever see.

I think it’s a bug and I think bugs must be fixed!

For breaking changes You can use major versions. To detect breaked of functionality we have testing.

Everything else - recognition of one’s own incompetence and justification of laziness.

Best regards, Boris.

11reactions
anjmaocommented, Sep 23, 2019

You can have a value something like -1 etc for such case and will will work just fine. If we add this change it could break a lot of functionality since we treat both null and undefined as empty or cleared state.

items = [
   { label: 'Select all', value: -1 }
   { label: 'One', value: 1 }
]

onChange(value) {
   if (value === -1) {
       // Treat the same as with null.
   }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

AngularJS null value for select - Stack Overflow
When the page is loaded, the first option is selected, which is good, and the output is {"selected":null} . When that first option...
Read more >
Angular select/ng-options with '' instead of null? - Google Groups
The option value is explicitly empty string, not null. Peter, your fix is clever and works, but it's remedial, restoring the normal behavior...
Read more >
Angular ng-select - npm
The first argument is a value from an option. The second is a value from the selection(model). A boolean should be returned. dropdownPosition ......
Read more >
NgFor Select list with null option : r/Angular2 - Reddit
So on data bind, that "Please Select" option wouldn't be selected automatically. And then posting the empty string to the server would cause...
Read more >
empty option in ng-model - ServiceNow Community
The empty option is generated when a value referenced by ng-model doesn't exist in a set of options passed to ng-options. This happens...
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