ng-option with null value causes empty selection
See original GitHub issueDescribe 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
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>
Issue Analytics
- State:
- Created 5 years ago
- Reactions:34
- Comments:20 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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.