Вы можете добиться желаемого поведения, создав пользовательскую директиву:
открытый modal.directive.ts
@Directive({
selector: '[openModal]'
})
export class OpenModalDirective {
@Input() modalIdentifier: string;
constructor(private modalService: ModalService) { }
@HostListener('click', ['$event'])
clickEvent(event) {
event.preventDefault();
event.stopPropagation();
this.modalService.openModal(this.modalIdentifier);
}
}
пустышка open-modal.service.ts
@Injectable({
providedIn: 'root'
})
export class ModalService {
constructor() {}
openModal(modalIdentifier: string) {
console.log(modalIdentifier)
}
}
Директива может использоваться следующим образом:
<button openModal [modalIdentifier]="'helloComponent'">Open Modal</button>
Этот пример основан на статье Кори Райлана , а доступен на StackBlitz .