How can I access to that singleton instances inside angular dependencies injections?
I want to write custom decorator for confirm dialog using this way.
Decorator implementation:
import {NgplDialogConfirmModel, NgplDialogService} from '../dialog';
import {take} from 'rxjs/operators';
export function Confirmable(options?: NgplDialogConfirmModel, confirm = 'confirmService') {
// our factory function will return our actual decorator function, but now we have
// an actual options object to configure our alert box :)
return (target: Object, propertyKey: string, descriptor: PropertyDescriptor) => {
// the usual, caching the original implementation
const originalMethod = descriptor.value;
// default values for our config, we’ll overwrite this with our options parameter
console.log('target', target);
console.log('propertyKey', propertyKey);
console.log('descriptor.value', descriptor.value);
console.log('target[confirm]', target[confirm]);
const service: NgplDialogService = target && target[confirm];
if (!service) {
return descriptor;
}
descriptor.value = function(...args: any[]) {
const dialogRef = service.confirm(options);
return dialogRef
.pipe(
take(1)
)
.subscribe((confirmed: boolean) => {
if (!!confirmed) {
originalMethod.apply(this, ...args);
}
});
};
return descriptor;
};
}
Used Here
@Confirmable()
confirmDelete(action): void {
console.log('action', action);
}
I have my own service integrate wit matDialog and confirm actions I use from everywhere.
@Injectable({
providedIn: 'root'
})
export class NgplDialogService {
constructor(private dialog: MatDialog) {
}
public confirm(data: any= {}): Observable<boolean> {
data.title = data.title || 'Confirm';
data.message = data.message || 'Are you sure?';
data.showCancel = data.showCancel || true;
data.showIcon = data.showIcon || true;
let dialogRef: MatDialogRef<ConfirmComponent>;
console.log('this.dialog', this.dialog);
dialogRef = this.dialog.open(ConfirmComponent, {
width: '480px',
disableClose: true,
data,
backdropClass: 'confirm-backdrop-class',
panelClass: 'confirm-panel-class'
});
return dialogRef.afterClosed();
}
}
I tried some approach like Injertor.create and ReflectiveInjector(deprecated) and i can't access to my service instance.
I tried to access to confirmService injected inside component from decorator, but decorator executed before component dependency injection finish.
After read several time the same articles on internet, once again I read this one, https://medium.com/capital-one-tech/custom-typescript-decorators-using-rxjs-and-angular-services-6ddd1079f683, and I understood better that approach and I tried it in my stackbliz code, and Its works. https://stackblitz.com/edit/angular-ivy-2bpgfc?file=src%2Fapp%2Fapp.component.ts