Created
February 19, 2020 12:42
-
-
Save MaZderMind/bf55ddbabcfcc6a6b0e658f895e0f065 to your computer and use it in GitHub Desktop.
Angular Component with Callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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