ngx-href
A library that allows href to understand Angular's router while retaining its default functionality.
- Use
router.navigate()
for internal link - Support scroll with the
#
attributes and let you configure the scrolling logic - Automatically append
rel="nooepener"
& target="_blank"
to external link if wished so - Support using
href
with the html button
attribute - Enable easy
Scroll when ready
mechanism which works with SSR
- Let you transform text to well formatted
anchor
Demo
19.0.0 Breaking change
- Now use
scrollIntoView
which render the Offset
useless. - Please use scroll-margin-top instead.
- Nows also work on SSR
Installation
npm install ngx-href
Inside your app.module.ts
file.
import { NgxHrefModule } from 'ngx-href'
imports: [
NgxHrefModule.forRoot({}),
NgxHrefModule.forRoot({
avoidSpam: true,
behavior:"smooth",
block:"center",
inline:"nearest",
rel:"noopener nofollow",
retryTimeout: 300,
target:"_blank",
}),
],
Angular routing
Nothing to do it should work out of the box
Avoid Spam
- Change the
href
from the DOM from example@outlook.com
into example(at)outlook.com
- Let js handle the click event.
Scroll logic
Behavior
Default: "auto"
Accepted value: ScrollBehavior
// ("auto" | "instant" | "smooth")
Can also be passed individually directly through html
<a href="https://my-external-url.com" behavior="instant">
Offset
If you wish to add offset, add scroll-margin-top: $offset
to your targeted component -> read more
retryTimeout
Default: undefined
Accepted value: number
Trigger a second scrollTo
event after retryTimeout
milliseconds.
Note: This should be avoided, prefer playing with skeleton and fixed height
External link
Rel attribute
Default: undefined
Accepted value: string
Can also be passed individually directly through html
<a href="https://my-external-url.com" rel="noopener nofollow">
Target attribute
Default: "_self"
Accepted value: string
Can also be passed individually directly through html
<a href="https://my-external-url.com" target="_blank">
Usage
Wherever you plan to use the href directive or pipe
import { NgxHrefDirective, ToAnchorPipe } from 'ngx-href'
imports: [
NgxHrefDirective,
ToAnchorPipe,
]
Then you can use it as you would normally use an a
element
Directive
Normal use
<a href="/angular/router/link">
My internal link
</a>
<button href="/angular/router/link">
My internal link
</button>
<a href="https://external-url.com">
An external link
</a>
<a href="tel:+41791112233">
+41791112233
</a>
<a href="mailto:foobar@outlook.com">
foobar@outlook.com
</a>
<a href="#myAnchor">
My scroll to anchor
</a>
<a href="/angular/router#link">
My internal link with anchor
</a>
Pipe: ToAnchorPipe
The toAnchor
pipe let you
-
transform an element ot a correct anchor
example: my Title $%
will be transform to my-title
-
Emit that this anchor have been created, so that we can scroll to that element
<div [id]="my Title $%"| toAnchor : false"> </div>
<div [id]="my Title $%"| toAnchor"> </div>
Service
import { NgxHrefService } from 'ngx-href'
constructor(public ngxHrefService: NgxHrefService) {}
Normal use
<button (click)="ngxHrefService.scrollTo(#myAnchor)">
Scroll to #myAnchor
</button>
<h2 id="myAnchor">A title</h2>
Authors and acknowledgment