src/app/utils/query-component/query.component.ts
This component is intended to be used in combination with an Observable
.
The component expects that either an ng-template
or a MatButton
is in the body and that the appropriate directive
is used on it (QueryBodyDirective with ng-template
, QueryButtonDirective with button
).
Once the appropriate function is called, the component subscribes to the observable, showing the user a loading
indication and, if applicable, disables the button
.
Once the query finished, the content in the ng-template
is shown or the button is enabled again.
If the subscription yielded an error, a message is shown instead of the ng-template
, or the button is re-enabled.
Note that no verification has to be made if the query was successful, when using an ng-template
.
The content of the ng-template
is only executed once the #queryState is Ready
.
ng-template
This example will show, instead of the content directly, a loading indicator as soon as the page is loaded.
button
This example will subscribe to the observable as soon as the button is clicked. The button is disabled and a loading indicator is shown as long as the subscription has not yielded a result.
selector | app-query-component |
styleUrls | query.component.scss |
templateUrl | query.component.html |
Properties |
|
Methods |
|
Inputs |
constructor(notify: UserNotifyService, changeDetector: ChangeDetectorRef)
|
|||||||||
Parameters :
|
errorMessage | |
Type : string
|
|
Default value : 'Failed to run query!'
|
|
Error message to be shown if the subscription failed |
Public listenTo | ||||||||||||||||
listenTo(query: Observable
|
||||||||||||||||
Type parameters :
|
||||||||||||||||
Subscribe the query component to the observable
Parameters :
Returns :
void
|
Public ready |
ready()
|
Quick way of checking if
Returns :
boolean
|
Public setError |
setError()
|
Manually sets this query component to the error state. This may be useful if e.g. the query ran successfully, the contained data in it however is not valid.
Returns :
void
|
Private updateButton |
updateButton()
|
Returns :
void
|
body |
Type : QueryBodyDirective
|
Decorators :
@ContentChild(QueryBodyDirective)
|
button |
Type : QueryButtonDirective
|
Decorators :
@ContentChild(QueryButtonDirective)
|
buttonMode |
Type : boolean
|
If true, a button is in the query body, not a template |
queryState |
Type : QueryComponentState
|
Default value : QueryComponentState.Loading
|
The current state of the query |
Private Optional subscription |
Type : Subscription
|
query.component.scss