src/lib/src/components/default-error.component.ts
Default view that is used when no other view has been matched.
To replace it, register own view with *
type nad status (don't forget responseType
):
ResourceRouterModule.forTypes([
{
component: MyGenericErrorComponent,
status: '*',
type: '*',
responseType: 'text'
}
]);
If you want to have custom view only for specific content type (e.g. specific defined error), register it as follows:
ResourceRouterModule.forTypes([
{
component: MyApiErrorComponent,
status: '*',
type: 'application/vnd.company.error',
responseType: 'json'
}
]);
Note: since responseType: 'json'
is default, it can be omitted.
Both above views can co-exist - when well-defined error is received, MyApiErrorComponent
is used,
and for unexpected there is MyGenericErrorComponent
.
templateUrl | ./default-error.component.html |
Properties |
Methods |
Accessors |
constructor(view: ActivatedView
|
||||||||
Parameters :
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
data |
data:
|
Type : ViewData<any>
|
Public view |
view:
|
Type : ActivatedView<any>
|
html |
gethtml()
|
object |
getobject()
|
import { Component, OnInit } from '@angular/core';
import { ActivatedView } from '../activated-view';
import { ViewData } from '../view-data';
/**
* Default view that is used when no other view has been matched.
*
* To replace it, register own view with `*` type nad status (don't forget `responseType`):
* ```ts
* ResourceRouterModule.forTypes([
* {
* component: MyGenericErrorComponent,
* status: '*',
* type: '*',
* responseType: 'text'
* }
* ]);
* ```
*
* If you want to have custom view only for specific content type (e.g. specific defined error), register it as follows:
* ```ts
* ResourceRouterModule.forTypes([
* {
* component: MyApiErrorComponent,
* status: '*',
* type: 'application/vnd.company.error',
* responseType: 'json'
* }
* ]);
* ```
* Note: since `responseType: 'json'` is default, it can be omitted.
*
* Both above views can co-exist - when well-defined error is received, `MyApiErrorComponent` is used,
* and for unexpected there is `MyGenericErrorComponent`.
*/
@Component({
templateUrl: './default-error.component.html'
})
export class DefaultErrorComponent implements OnInit {
data: ViewData<any>;
constructor(public view: ActivatedView<any>) {
}
ngOnInit(): void {
this.view.data.subscribe(data => this.data = data);
}
get html() {
return this.data.type === 'text/html';
}
get object() {
return typeof this.data.body === 'object';
}
}
<p>
Error loading a view<code *ngIf="data.type"> {{data.type}}</code>
from resource <code>{{data.url}}</code> with <code>{{data.status}} {{data.statusText}}</code>
</p>
<ng-container *ngIf="data.body as body">
<!-- Plain text -->
<pre *ngIf="!html; else htmlTemplate">{{object ? (body|json) : body}}</pre>
<!-- HTML -->
<ng-template #htmlTemplate>
<!-- Note: body is automatically sanitized by angular,
which may lead to displaying some garbage from head element,
but its perfectly safe to display it - NEVER use DomSanitizer.bypassSecurityTrustHtml -->
<div [innerHTML]="body"></div>
</ng-template>
</ng-container>