Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 - Using (click) instead of <a href="#target"></a>

I have a link which opens a modal dialog:

<a href="#openModal">Open Modal</a>

But I want to use angular (click) or similar to open the modal instead.

How can I do this?


2 Answers

Maybe this is what you are looking for, You can reference the window object inside you component:

 private window: any = window;

and then in your template use this:

 <button (click)="window.location.href = '#openModal'">Open Modal</button>

but remember as described here

Referencing global browser objects like document or window directly from within your code is possible, but not encouraged and considered bad practice.

and it's recommended that you register WindowRef as provider.

like image 112
KavehG Avatar answered Nov 24 '25 07:11

KavehG


Do something like this:

<button (click)="openModal()">Open Modal</button>

In openModal:

openModal() {
  window.location.href = 'yoururl';
}
like image 35
Kyle Krzeski Avatar answered Nov 24 '25 06:11

Kyle Krzeski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!