Skip to content
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

feat(autocomplete): fire "onSelected" event when the user select an option #3645

Closed
jefersonestevo opened this issue Mar 16, 2017 · 8 comments · Fixed by #4187
Closed

feat(autocomplete): fire "onSelected" event when the user select an option #3645

jefersonestevo opened this issue Mar 16, 2017 · 8 comments · Fixed by #4187
Assignees
Labels
feature This issue represents a new feature or feature request rather than a bug or bug fix P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@jefersonestevo
Copy link
Contributor

Bug, feature request, or proposal:

featur

What is the expected behavior?

Create a new output (onSelected?) that fires an event when the user select an option from the autocomplete

What is the current behavior?

There is no way to know when the user has selected an option from the autocomplete

What is the use-case or motivation for changing an existing behavior?

I have some cases that I need to update some fields based on the user selected option and would be nice if I can get a callback from the component itself

@jelbourn jelbourn added the feature This issue represents a new feature or feature request rather than a bug or bug fix label Mar 16, 2017
@jelbourn jelbourn added the P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent label Mar 16, 2017
@willshowell
Copy link
Contributor

There is an optionSelections Observable on the autocomplete component. Does that accomplish what you need?

/** Stream of autocomplete option selections. */
get optionSelections(): Observable<MdOptionSelectionChange> {
  return Observable.merge(...this.autocomplete.options.map(option => option.onSelectionChange));
}

From https://github.com/angular/material2/blob/master/src/lib/autocomplete/autocomplete-trigger.ts#L160

@jefersonestevo
Copy link
Contributor Author

I haven't tested, but I think that this observer would work for what I'm needing. But the point is that it'll be nicer if we can just create a listener on our class and use the event listener right from the html page.

<md-autocomplete (onSelected)="myCallbackFunction($event)" .....

@willshowell
Copy link
Contributor

You can also wire up a listener to md-option if you'd like. See #3576.

Note: the api for md-option changes as of #2722 to support multi select (in case you're using the latest builds)

@jefersonestevo
Copy link
Contributor Author

Great! I can use that for now. Thanks @willshowell

@jefersonestevo
Copy link
Contributor Author

@kara do you think that "onSelected" will be a good name for the output event? If so, I can create a PR with this change

@wulfsberg
Copy link

Just came across this randomly, and wanted to mentioned that the official styleguide discourages the "on-" prefix for output events.
https://angular.io/docs/ts/latest/guide/style-guide.html#!#05-16

crisbeto added a commit to crisbeto/material2 that referenced this issue Apr 20, 2017
Emits the `select` event when an option in the autocomplete is selected.

**Note:** I went with passing the selected option from the trigger to the panel, instead of listening to the `onSelectionChange` inside the panel, because it involves keeping track of less subscriptions and not having to re-construct them when the list of options changes.

Fixes angular#4094.
Fixes angular#3645.
@liri2006
Copy link

liri2006 commented Jul 2, 2017

+1

crisbeto added a commit to crisbeto/material2 that referenced this issue Aug 20, 2017
Emits the `select` event when an option in the autocomplete is selected.

**Note:** I went with passing the selected option from the trigger to the panel, instead of listening to the `onSelectionChange` inside the panel, because it involves keeping track of less subscriptions and not having to re-construct them when the list of options changes.

Fixes angular#4094.
Fixes angular#3645.
crisbeto added a commit to crisbeto/material2 that referenced this issue Aug 23, 2017
Emits the `select` event when an option in the autocomplete is selected.

**Note:** I went with passing the selected option from the trigger to the panel, instead of listening to the `onSelectionChange` inside the panel, because it involves keeping track of less subscriptions and not having to re-construct them when the list of options changes.

Fixes angular#4094.
Fixes angular#3645.
@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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature This issue represents a new feature or feature request rather than a bug or bug fix P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants