ng-select: ng-option with null value causes empty selection
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
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>
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 38
- Comments: 23 (2 by maintainers)
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.
Same problem here…
I was dealing with this issue and found a super easy work around. Just add placeholder to your ng-select:
<ng-select [(ngModel)]="foo" (ngModelChange)="generateReport()" [clearable]="false" placeholder="Show All" class="baz">If you don’t like the lighter color of the placeholder, add this to your css:
::ng-deep .ng-select.baz .ng-select-container .ng-value-container .ng-placeholder { color: #333; }Currently, we treat
nullandundefinedas empty values. E.g.: user clears selection and hisngModelis null.We probably need to keep
nullas a valid value andundefinedas an empty value since we need to address empty state somehow 🤔Any progress on this?
@joegitau What was being discussed here is exactly this, whatever number/value you use, it’s completely diferent from “null”. It can’t be acceptable as solution.
Hi, guys. As I can understand problem is in this file on string 227
const value = isDefined(item.$ngOptionValue) ? item.$ngOptionValue : item;It works wrong because of isDefined method looks likeexport function isDefined(value: any) { return value !== undefined && value !== null; }I think null is defined value and this behavior must be fixed. P.S. Sorry for my bad English.Any update on this? I see the PR is stale for last 2 weeks, Can I be of any help to expedite this?
I was stuggling with same issue but the below solution works well for me:
It’s just
selecttag instead ofng-selectbut resolves the issue. Dunno if it works forng-selectas well.You could first bind the Show All value to some number, say 0. Then convert 0 to null within your class definition - tapping into the
Changeoutput event!So, something like…
Then within the template class…