-
Notifications
You must be signed in to change notification settings - Fork 912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error when using object values in ng-option with reactive forms #414
Comments
Hi @wkiril <ng-select
placeholder="Select city"
bindLabel="name"
formControlName="city">
<ng-option
*ngFor="let city of cities"
[value]="city">{{ city.name }}
</ng-option>
</ng-select> I see that it would be good to add validation for inputs so end user can better see that is required at which case. |
can't the value inside the ng-option tags be used as label? my use case is a little bit different: <ng-select formControlName="city">
<ng-option
*ngFor="let city of cities"
[value]="city"
>{{ city.name }}</ng-option>
<ng-option
*ngIf="form.value.city === 'custom'"
[value]="'custom'"
>Custom city</ng-option>
<ng-template ng-footer-tmp>
<a (click)="openCustomCityModal()">Custom city</a>
</ng-template>
</ng-select> so the values are different - an object and a simple string. it could be done with a normal select but I need the footer template |
I also noticed that using bindLabel without [items] (as in your example) doesn't show the label |
You are correct, option innerHTML can be used as label and bindLabel can be ignored. I looked at the code and it needs to be refactored a bit to support this case. Implementation note: |
@emmanueldaher Yes it is available. |
yes, thanks @anjmao for the quick replies. I know the workarounds |
there's still an inconsistency using a simple string ng-option and reactive forms: https://stackblitz.com/edit/ng-select-xsyqyl the initial value is displayed instead of the label from the ng-option innerHTML |
this is also observable in the default demo page https://ng-select.github.io/ng-select#/forms |
@wkiril Thanks for good example on inconsistency 👍 |
thank you for the fix. however now the html doesn't render on the Basic select in Reactive forms demo page - https://ng-select.github.io/ng-select#/forms |
got an error |
Expected behaviour
It should be possible to use ng-option component with an object in [value] using the reactive forms
Actual behaviour
https://plnkr.co/edit/m0oMVG6uvfHKp5ai3uSL?p=preview
Here's the error
Cannot read property 'indexOf' of undefined
at ItemsList.resolveNested (ng-select.js:1207)
at ItemsList.mapItem (ng-select.js:1223)
at select (ng-select.js:2110)
at NgSelectComponent._handleWriteValue (ng-select.js:2125)
at NgSelectComponent.writeValue (ng-select.js:1874)
at setUpControl (forms.js:2300)
at FormGroupDirective.addControl (forms.js:6659)
at FormControlName._setUpControl (forms.js:7309)
at FormControlName.ngOnChanges (forms.js:7222)
at checkAndUpdateDirectiveInline (core.js:12396)
More Info
ng-select version: 0.35.0
browser: Chrome
reproducible in demo page: NO
The text was updated successfully, but these errors were encountered: