I achieve it by creating a component for <a>
, confirmation dialog component, and service for the dialog
The confirm dialog
I'm using Angular Material
import { Component, Inject, Output, EventEmitter } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
@Component({
selector: 'confirm-dialog',
templateUrl: './confirm-dialog.component.html',
})
export class ConfirmDialogComponent {
constructor(
public translate:TranslateService,
public dialogRef: MatDialogRef<ConfirmDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: any
) {
}
onClick(result): void {
this.dialogRef.close(result);
}
}
the html file
<h1 mat-dialog-title>{{data.title}}</h1>
<div mat-dialog-content>
<h4 class="card-title">{{ data.message }}</h4>
</div>
<div mat-dialog-actions class="pull-right">
<a *ngIf="data.confirm_link" class="btn btn-primary" mat-button tabindex="-1" href="{{ data.confirm_link }}" target="_blank" (click)="onClick(true)">{{ data.confirm_button }}</a>
<button *ngIf="!data.confirm_link" class="btn btn-primary" mat-button tabindex="-1" (click)="onClick(true)"> {{ data.confirm_button }} </button>
<button class="btn btn-info" mat-button tabindex="-1" (click)="onClick(false)">Cancel</button>
</div>
The service
Once the component is created, I want to make it easy to call from anywhere, so create a service for it
import { Injectable, OnDestroy} from "@angular/core";
import { Subject } from 'rxjs/Subject';
import { MatDialog } from '@angular/material';
import { ConfirmDialogComponent } from 'path/to/confirm-dialog/confirm-dialog.component';
import * as _ from 'lodash';
@Injectable()
export class ConfirmService implements OnDestroy{
private subject = new Subject<any>();
private message = 1;
info: any;
constructor(private dialog: MatDialog){
}
show(data: any){
let dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '500px',
data: data,
});
dialogRef.afterClosed().subscribe(result => {
this.subject.next(result);
});
return this.subject;
}
ngOnDestroy() {
}
}
The custom <a>
element
To make it easier to use in .html file, I create a component for it
import { Component, OnInit, Input } from '@angular/core';
import { ConfirmService } from 'path/to/service/confirm.service';
@Component({
selector: 'a-external',
templateUrl: './a-external.component.html',
})
export class AExternalComponent implements OnInit {
@Input('href') href: string;
@Input('class') classes: string;
@Input('content') content: string;
constructor(
private confirmService:ConfirmService,
) { }
ngOnInit() {
}
onAClick() {
var dialog = this.confirmService.show({
'title': 'Warning',
'message': 'This will open a new tab',
'confirm_button': 'open',
'confirm_link': this.href, // if pass in the uri, will open in new tab
});
var subscription = dialog.subscribe((result) => {
// if the result is true, means Confirm button is clicked
// if the result is false, means Cancel button is clicked
subscription.unsubscribe();
});
}
}
The confirm_link
is only applicable for open a new tab. Without the value, it will just trigger the dialog subscription result.
And the html file is very simple
<a href="javascript:" class="{{ classes }}" (click)="onAClick()">{{ content }}</a>
To use it
<a-external [href]="http://www.foobar.com" [class]="'btn btn-info'" [content]="'The content inside a element'"></a-external>
CanDeactivate
route guard? – Oven