ng2-select icon indicating copy to clipboard operation
ng2-select copied to clipboard

Make it ngModel (@angular/forms) compliant?

Open achimha opened this issue 9 years ago • 32 comments

The component is not ngModel compliant and cannot easily be used in a form with validation. To make it compliant, it should implement an accessor interface, similar to what is shown here (which is not RC1 compatible): http://restlet.com/blog/2016/02/17/implementing-angular2-forms-beyond-basics-part-2/

Are there plans to make it compatible?

achimha avatar Jun 03 '16 07:06 achimha

To be more specific, rather look at this link: http://almerosteyn.com/2016/04/linkup-custom-control-to-ngcontrol-ngmodel It's shorter and narrowed down to the topic.

The job is to extend SelectComponent with ControlValueAccessor interface implementation.

It's basically a matter of implementing those methods in there:

writeValue(obj: any): void {
  // set selected values
}

registerOnChange(fn: any): void {
  // simply save callback which will be called when `selected` changes
}

registerOnTouched(fn: any): void {
  // save a callback which will be called when control is touched (clicked)
}

@valorkin I'll make the PR but it would be useful to have #223 merged in.

Namek avatar Jun 07 '16 08:06 Namek

Here's my branch to be PR-ed: development---set_selected_items/feat---control_value_accessor/feat - it's based on #223.

Namek avatar Jun 07 '16 10:06 Namek

actually there are plans to make in compliant with NgModel and NgControl

valorkin avatar Jun 08 '16 16:06 valorkin

and what about my branch? Isn't that enough?

Namek avatar Jun 08 '16 21:06 Namek

I've got a problem because of that. Any progress?

kamikaze avatar Jul 05 '16 23:07 kamikaze

I need a way of two way data binding due to the ng-select directive being part of a form that is loaded in a child route where the parent route contains an ID parameter. Whenever the parent route's ID parameter changes the selected items of the ng-select directive should update accordingly though nothing I have tried accomplishes this.

The documentation seems to suggest [data]="selectedItems" should work but this is not the case, the only way I can get ng-select to load data is by using [initData]="selectedItems" but that only works once and doesn't listen for changes.

The only way I could think is to force the form's component to disallow reuse but that's dirty!

jasonroyle avatar Jul 07 '16 10:07 jasonroyle

Hi,

I've made this component ngModel compliant with few lines of codes here : https://github.com/chymz/ng2-select/commit/0b79ec583b8f5de763728fdee9550fb0641077de

It seems to work perfectly with the last RC4

[Edit] Here is a pull request : https://github.com/valor-software/ng2-select/pull/299

yabab-dev avatar Jul 11 '16 20:07 yabab-dev

at the moment I am updating and publishing ng2-bootstrap, after it I will get here

valorkin avatar Jul 11 '16 20:07 valorkin

please make it tslint compatible too :)

kamikaze avatar Jul 13 '16 14:07 kamikaze

You may want to consider to make it compliant with formControlName too. Even more important since ngModel seems now to be considered in the new forms as an edge case (i.e. ngModel is intended for standalone input while formControlName is the real control part of a form). https://docs.google.com/document/u/0/d/1RIezQqE4aEhBRmArIAS1mRIZtWFf6JxN_7B4meyWK0Y/pub

0cv avatar Jul 25 '16 21:07 0cv

Well, my pull request make this component compatible with template-driven forms (ngModel) and model-driven forms too (formGroup/formControlName)

yabab-dev avatar Jul 25 '16 21:07 yabab-dev

Little is happening... Any chance to have that reviewed/merged/released?

0cv avatar Aug 05 '16 07:08 0cv

Add my vote, ngModule is a must, otherwise all the cool stuff of Angular are worthless. Thanks :)

ghost avatar Aug 16 '16 15:08 ghost

+1

k1ng440 avatar Oct 08 '16 09:10 k1ng440

can you try typeahead from ng2-bootstrap?

valorkin avatar Oct 10 '16 09:10 valorkin

+1

elmartino avatar Dec 04 '16 19:12 elmartino

+1

christianecker avatar Dec 15 '16 15:12 christianecker

+1

sveatlo avatar Dec 17 '16 23:12 sveatlo

👍

lanceschi avatar Dec 22 '16 12:12 lanceschi

Any movement on this at all?

yairgo avatar Dec 27 '16 17:12 yairgo

+1

toke182 avatar Dec 29 '16 01:12 toke182

Fix in https://github.com/valor-software/ng2-select/pull/575

toke182 avatar Jan 03 '17 17:01 toke182

@toke182 so is this compliant with angular 2 forms validation? I'm sorry, I didn't quite understand

blubberbo avatar Feb 14 '17 00:02 blubberbo

@blubberbo Yes, It got merged to development branch some time ago

toke182 avatar Feb 20 '17 01:02 toke182

@toke182 is there an example of an implementation somewhere I could take a look at maybe?

blubberbo avatar Feb 20 '17 21:02 blubberbo

This feature would be greatly appreciated.

BBaysinger avatar May 19 '17 16:05 BBaysinger

Yes, it would be greatly appreciated!

michelebombardi avatar Jun 08 '17 15:06 michelebombardi

Just checking here, just tried to bind to ngModel and getting an error that it's not a property of select2. Was any progress made on making this component ngModel compliant?

bryanbanda avatar Jun 14 '17 18:06 bryanbanda

ng2-select-compat seems to be ngModel complaint but I keep getting the following error when there are more then 1 ng-select on one page, the first ng-select on the page works. All the subsequent ones do not.:

ERROR TypeError: selectedItems.map is not a function
    at SelectComponent.set [as active] (http://localhost:4200/vendor.bundle.js:52879:46)
    at SelectComponent.webpackJsonp.../../../../ng2-select-compat/select/select.js.SelectComponent.writeValue (http://localhost:4200/vendor.bundle.js:53024:21)
    at http://localhost:4200/vendor.bundle.js:143067:29
    at http://localhost:4200/vendor.bundle.js:144253:65
    at Array.forEach (<anonymous>)
    at FormControl.webpackJsonp.../../../forms/@angular/forms.es5.js.FormControl.setValue (http://localhost:4200/vendor.bundle.js:144253:28)
    at FormGroupDirective.webpackJsonp.../../../forms/@angular/forms.es5.js.FormGroupDirective.updateModel (http://localhost:4200/vendor.bundle.js:146081:14)
    at FormControlName.webpackJsonp.../../../forms/@angular/forms.es5.js.FormControlName.ngOnChanges (http://localhost:4200/vendor.bundle.js:146530:32)
    at checkAndUpdateDirectiveInline (http://localhost:4200/vendor.bundle.js:137306:19)
    at checkAndUpdateNodeInline (http://localhost:4200/vendor.bundle.js:138797:17)

bassrock avatar Jun 16 '17 21:06 bassrock

New life the component is there: https://github.com/optimistex/ng2-select-ex

optimistex avatar Feb 03 '18 23:02 optimistex