I have [routerLink]
as below;
<li *ngFor="let item of pageNumbers" class="page-item">
<a
class="page-link"
[routerLink]="getPageUrl(item) | async"
routerLinkActive="active"
>{{ item }}</a
>
</li>
And getPageUrl
method is like;
public getPageUrl(pageNumber: number): Observable<string> {
const url: Observable<string> = this.route.url.pipe(
map((segments: UrlSegment[]) => {
let segmentsString = segments.join("/");
let pageUrl = `/${segmentsString}?startIndex=${pageNumber}`;
return pageUrl;
})
);
return url;
}
But on browser angular shows url like below;
http://localhost:3005/documents%3FstartIndex%3D1
desired:
http://localhost:3005/documents?startIndex=1
Am I missing something here? I would simply bind pagination directly to [routerLink], but there will be more query string parameters (filters, sorting) will get in to URL, that’s why I am trying to generate URL dynamically.
What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.
Yes it can be attached to div tag, your route is probably wrong try add / in front of route.
Href is the basic attribute provided by Html to navigate through pages which reloads the page on click. routerLink is the attribute provided by angular to navigate to different components without reloading the page.
RouterLink is a built-in Angular Directive that lets you link to specific routes in your app. In the SPA(single-page application), you change what the user sees by showing or hiding portions of the display that correspond to particular components, rather than going out to the server to get a new page.
Check the docs for RouterLink:
[routerLink]
is used to pass what comes before the ?
, called "params".[queryParams]
is used to pass what comes after the ?
, called "query params".So, to solve your issue, a possible solution is to return an object from getPageUrl
instead of a string, the object will have both the params and query params:
public getPageUrl(pageNumber: number): Observable<string> {
return this.route.url.pipe(
map((segments: UrlSegment[]) => {
return {params: segments, queryParams: {startIndex: pageNumber};
})
);
}
and you use it like this:
<a *ngIf="getPageUrl(item) | async as url"
class="page-link"
[routerLink]="url.params"
[queryParams]="url.queryParams">{{ item }}</a
Notice you don't need to join the query segments with /
, because routerLink
accepts an array too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With