1

I am using the ng2-select component to extend another component - I called it my-dropdown-field. However the ng2-select component does not render for some reason. The issue does not seem to be in the ng2 control, but rather in the wrapper control I created. I am however not able to figure out what is causing this. There are no errors thrown either. If I use ng2-select on it's own its working fine. I created a reproducible stackblitz.

https://stackblitz.com/edit/ng2-select-angular2

If you run this, you will see there is no ng2-select control being rendered. If you comment out the below section from the app.component.html:

Doesn't work

<form [formGroup]="myGroup" (submit)="showForm()">
    <my-dropdown-field 
    name="country"
    [items]="items"
    [selectedItem]="active"
    formControlName="country">
    </my-dropdown-field>
  <button (click)="showForm()">Show</button>
</form>

and uncomment this from the top:

Works

<form [formGroup]="myGroup" (submit)="showForm()">
    <ng-select 
    name="country"
    [(ngModel)]="active"
    [(active)]="active"
    [items]="items"
    formControlName="country">
    </ng-select>
  <button (click)="showForm()">Show</button>
</form>

Then you will see that it works just fine.

Here is the link to the original ng2-select component:

https://github.com/valor-software/ng2-select

What am I missing?

2 Answers 2

1

Your code doesn't work because you're overwriting ng2-select's property items. Originally it's a setter (https://github.com/valor-software/ng2-select/blob/development/src/select/select.ts#L269) so this line @Input() items: Array<string> = []; is changing original behavior.

A workaround in this situation is to use different name for this property (options, objects, etc) Here's working example https://stackblitz.com/edit/ng2-select-angular2-tof6ko?file=app/app.component.html

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I see that was the issue. I also had to add the bootstrap.min.css. The stackblitz has been updated.
0

MyDropdownFieldComponent should implement ControlValueAccessor if you want that it works with ngModel and reactive forms.

[selectedItem] should not be an array or change the name

On AppComponent, create the form in ngOnInit instead of the constructor

2 Comments

MyDropdownFieldComponent extends SelectComponent which implements ControlValueAccessor. Do I still need to manually implement it again? selectedItem is actually an array, according to the ng2-select documentation.
I think you don't need to implement the interface again, but you can override the methods of the interface according to your needs. I am reading: valor-software.com/ng2-select Try to double check every input and output and avoid double binding. Whatever is on: country: new FormControl('', []) should initialize the dropdown. If you put: country: new FormControl('Australia', []). Your active should be Australia. Australia should appear on writeValue method if you do a console log there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.