8000 fix(forms): provide a method to compare options by DzmitryShylovich · Pull Request #13349 · angular/angular · GitHub
[go: up one dir, main page]

Skip to content

Conversation

DzmitryShylovich
Copy link
Contributor
@DzmitryShylovich DzmitryShylovich commented Dec 9, 2016

Closes #13268 and #13274 / #13828 / #13884 #14121
Usage:

<select [compareWith]="equals"  [(ngModel)]="selectedCountries">
   <option *ngFor="let country of countries" [ngValue]="country">
      {{country.name}}
   </option>
</select>

equals(o1: Country, o2: Country) {
   return o1.id === o2.id;
}

@vicb vicb added area: forms action: review The PR is still awaiting reviews from at least one requested reviewer labels Dec 9, 2016
@@ -82,6 +82,8 @@ export class SelectControlValueAccessor implements ControlValueAccessor {
onChange = (_: any) => {};
onTouched = () => {};

@Input() ngEquals: (o1: any, o2: any) => boolean = looseIdentical;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compareOptions/ equalOptions ?
compare implies 1, 0, -1 return... probably overkill.

Copy link
Contributor Author
@DzmitryShylovich DzmitryShylovich Dec 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compare implies 1, 0, -1 return

yeah, this is why I chose equals. We don't need to sort it.

@vicb
Copy link
Contributor
vicb commented Dec 9, 2016

LGTM, @kara could you please review ?

Copy link
Contributor
@kara kara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test for this?

@@ -82,6 +82,8 @@ export class SelectControlValueAccessor implements ControlValueAccessor {
onChange = (_: any) => {};
onTouched = () => {};

@Input() equalOptions: (o1: any, o2: any) => boolean = looseIdentical;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I like the idea of an optional equality method, but I don't think this name is very intuitive. Perhaps compareWith? Discussed with @StephenFluin and we think that might fit the use case better.

<select [compareWith]="compareFn" [(ngModel)]="someProp">
   ...
</select>

@kara kara added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Dec 12, 2016
@DzmitryShylovich
Copy link
Contributor Author

@kara ready for review

Copy link
Contributor
@kara kara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kara kara added action: merge The PR is ready for merge by the caretaker and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Jan 30, 2017
@mhevery mhevery closed this in f89d004 Jan 31, 2017
asnowwolf pushed a commit to asnowwolf/angular that referenced this pull request Aug 11, 2017
juleskremer pushed a commit to juleskremer/angular that referenced this pull request Aug 28, 2017
@JosephBL
Copy link
JosephBL commented Oct 2, 2017

Is a release date known for this feature? It would help a lot :)

@another-guy
Copy link

@JosephBL this feature has been released for many months. (since 4.0.0-beta.6 : https://github.com/angular/angular/blob/master/CHANGELOG.md#400-beta6-2017-02-03 "forms: provide a method to compare options (#13349) (f89d004), closes #13268" )

@JosephBL
Copy link
JosephBL commented Oct 9, 2017

Thanks a lot, I somehow missed it :$

@rawatfeb
Copy link

Thanks a lot

@chinthu
Copy link
chinthu commented Apr 6, 2018

Faced an issue with this compare, if the select is inside the form /formgroup, with name , compare is not working

@marticrespi
Copy link

As @chinthu said, if the select is in a form the compare function doesn't work properly.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

934A
@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: forms cla: yes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide customizable object comparison method for NgModel
10 participants
0