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

Created on 16 Mar 2017  路  8Comments  路  Source: angular/components

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

P3 feature has pr

Most helpful comment

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

All 8 comments

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

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)" .....

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)

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

@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

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

+1

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._

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kara picture kara  路  3Comments

vitaly-t picture vitaly-t  路  3Comments

RoxKilly picture RoxKilly  路  3Comments

constantinlucian picture constantinlucian  路  3Comments

jelbourn picture jelbourn  路  3Comments