Angular: Enhancing URL links

Recently, I discovered a function in my code that allows me to cycle through different pictures and change the URL accordingly. The initial URL is obtained using angular routes, where the "domain" parameter consists of the domain.id and the domain.category.

<a [routerLink] = "['/image', image.id, domain]">

Initially, when I select the first image, everything works perfectly and I get the correct URL structure.

http://localhost:4200/image/1;id=1;category=testing.

However, upon calling the "next" function to move on to the next image, the resulting URL changes unexpectedly to:

"http://localhost:4200/image/2?id=1&category=testing.

So, my question is whether it's possible to replace the "?" and "&" characters with ";" in the URL.

next() {
  const next = this.activeId + 1 >= 9 ? 1 : this.activeId + 1;
  const extras: NavigationExtras = {
    queryParams: {
      id: this.domain.id,
      category: this.domain.catergory
    }
  };
  this.router.navigate(['/image/' + next], extras);
}

Answer №1

Implement string interpolation to achieve this

Utilize the navigateByUrl function for navigation

next() {
    const next = this.activeId + 1 >= 9  ? 1 : this.activeId + 1;
   this.router.navigateByUrl('/image/' + `${next};id=${this.domain.id};category=${this.domain.catergory}`);
}

Answer №2

If you opt for using queryParams (which I highly recommend), you can modify your initial example that utilizes [routerLink]. For instance, if you:

Revise

<a [routerLink] = "['/image', image.id, domain]">

Into

<a [routerLink] = "['/image']" [queryParams]="{imageId: image.id, domain: domain.id, category:domain.category}">

This will result in your first URL appearing as "http://localhost:4200/image/1?id=1"

By making this change, the functionality across your application will be much more cohesive.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: The JavaScript variable 'undefined' is being used as a function, which is incorrect. This error occurs when trying to execute the function `mockBackend

I am currently working on unit testing an AngularJS controller using Karma and Jasmine. Below is the test suite I have created: describe('Controllers', function(){ var $scope, ctrl; beforeEach(module('curriculumModule')); ...

Inspecting element value on a website

On a marketplace website, I extracted the attribute (capacity_gold) from a table. Since the values are dynamic and constantly changing, I aim to develop a basic script that will notify me when the attribute value exceeds 100. The current value of the attr ...

unable to submit form using angular and express

I am encountering an issue with a post request in AngularJS to express. The HTML form on the client side is defined as follows: <div ng-controller="LoginCtrl"> <form ng-submit="login()"> <div> <label>Username</lab ...

Copy and paste the code from your clipboard into various input fields

I have been searching for a Vanilla JavaScript solution to copy and paste code into multiple input fields. Although I have found solutions on the internet, they are all jQuery-based. Here is an example of such a jQuery solution <input type="text" maxl ...

Unexpected null value returned upon form submission with Mongoose and ExpressJS

I am encountering an issue with sending data from a basic HTML form to MongoDB using Express. When I try to post it, I am getting null as the result. The Schema I used is: commentname: String. Below is the snippet of the HTML code: <form id="form ...

Discover how to set up lazy loaded child routes within a parent route that is also loaded lazily in Angular 2

Struggling to implement lazy loading for my app has been a series of challenges. I successfully implemented lazy loading for the main route, /admin, but now I am facing issues while adding another route, /admin/login. This is what I tried: admin-router.m ...

"The issue with AngularJS ng-init, preventing the initialization of a variable at

For my project, I have implemented ng-repeat from the AngularJS directive to display an array containing JSON values with subarrays. <div ng-repeat="data in MENULIST" > //MENULIST contains an array of data I then perform certain conditions checks ...

The destroy method of Chart.js does not appear to have any impact on the

Hello, I've come across this issue in my react app that I need help with: this.chart = new Chart(node, options); // adding data to the chart ... this.chart.destroy(); this.chart = null; this.chart = new Chart(node, options); // adding data to the cha ...

What is the best way to reference an i18n entry within .json files?

I am in the process of creating a user interface using SAP UI5 and my goal is to make all text elements internationalized. Below is a snippet of my view in .xml format where I successfully retrieve text in different languages using placeholders enclosed i ...

Exploring the capabilities of AngularJS directives through ng-html2js testing

Many questions have been raised about the ng-html2js plugin, but unfortunately, none of the answers provided were able to solve my specific issue. I meticulously followed the official installation guide and also referenced the example available at https:/ ...

The combination of AddEventListener in the parent and postMessage in the iframe is ineffective

As I delve into the Stripe documentation to develop a new feature, I have encountered an issue regarding the communication between a webpage and an iframe. The code in index.html: <!DOCTYPE html> <body> parent<br> <iframe src="./ ...

How to toggle between displaying divs using JavaScript and Jquery

How can I use JavaScript to show or hide specific divs on page load and upon clicking different links? I want to display the "houseImages" div by default, while hiding the "landImages", "renovationImages", "UpcomingImages", and "voteForNext" divs. Then, w ...

The npm command returns an error message stating "Either an insufficient amount of arguments were provided or no matching entry was found."

I'm trying to pass a custom flag from an npm script to my webpack config, but I keep encountering the following error in the logs: Insufficient number of arguments or no entry found. Alternatively, run 'webpack(-cli) --help' for usage info. ...

Why is the validate function not being executed in the Backbone Model?

I am a beginner in learning Backbone and I am attempting to implement simple validation in my Person Model. However, I am facing an issue where the validate method does not run when I set a new age. Can someone guide me on where I might be making a mistake ...

What's the reason behind the refusal of my connection to localhost at port 3000 in Node.JS?

As a student venturing into the world of back-end development for the first time, I decided to dive into learning Node.JS. To kick things off, I downloaded a PDF book titled "Jumpstart Node.JS" from SitePoint. Following the provided instructions, I attempt ...

The Material UI Icon rendered using document.createElementNS in React is failing to load properly

I'm currently developing a tags section for my app, similar to the tags feature in a new Stack Overflow question form. For this, I am utilizing the Material UI close icon. You can see how it is implemented in this tutorial on YouTube. (I have confirme ...

How can I achieve a fade-in effect whenever the flag image is clicked?

A unique "international" quotes script has been created, showcasing Dutch, English, German, and French quotes. The script displays different quotes every day, with a draft-result visible in the upper right corner of this page ... The code used for this sc ...

What is the best way to ensure that the HTML page is only shown once all data from three JSON files has been fully

I have successfully parsed data from three different JSON files using the code below. //factory app.factory('myapp', ['$http', function($http) { function getLists() { var tab = [&a ...

The functionality of a Vue custom tooltip behaves strangely after clicking the button multiple times

I created this custom tooltip code that automatically closes after 2 seconds when a user clicks on a button, not just hovers over it. Initially, it works perfectly for the first two clicks, but then starts behaving strangely from the third click onwards. ...

In React Native, you can pass native component properties as props, while also defining custom ones using a TypeScript interface

I am working on a component called AppText where I need to utilize a Props interface and also be able to accept additional props that can be spread using the spread operator. However, I encountered this error: Type '{ children: string; accessible: tru ...