Skip to content

Instantly share code, notes, and snippets.

@MaZderMind
Created February 19, 2020 12:42
Show Gist options
  • Save MaZderMind/bf55ddbabcfcc6a6b0e658f895e0f065 to your computer and use it in GitHub Desktop.
Save MaZderMind/bf55ddbabcfcc6a6b0e658f895e0f065 to your computer and use it in GitHub Desktop.
Angular Component with Callback
export declare type QueryCallbackFunction = (results: string[]) => void;
declare type QueryFunction = (query: string, callback: QueryCallbackFunction) => void;
import {Component, Input} from '@angular/core';
@Component({
selector: 'app-example',
styleUrls: ['./example.component.scss'],
templateUrl: './example.component.html',
})
export class ExampleComponent {
/**
* Specify a Callback-Function which queries realtime results from a Server based on the entered Term.
* Usage example:
* <app-example
* …
* [queryFunction]="onQuery.bind(this)"
* ></app-example>
*
* onQuery(term: string, callback: QueryCallbackFunction): void {
* this.getResults(term).toPromise().then(results => {
* callback(results);
* });
* }
*/
@Input()
queryFunction: QueryFunction;
doSomething(term: string): void {
if (this.queryFunction) {
console.log('calling query function with', term);
this.queryFunction(term, (items) => {
console.log('query function returned', items);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment