What is the best way to prevent executing a method in [queryParams] in Angular 7?

I'm currently working on incorporating the option for users to open a link in a new tab. I'm trying to avoid using the (click) method, so I've come up with the following logic to achieve this:

<a [routerLink]="['/search/' + search.id]" [queryParams]="getQueryParams()">Destination</a>

The issue I'm facing is that the getQueryParams() function is being called every time, causing the app to slow down significantly. Is there a way to avoid calling this method every time? Additionally, I'm also open to exploring alternative methods to implement the open in a new tab functionality without relying on the (click) event?

Answer №1

Why is it necessary to retrieve the queryParams using a function?

If not, you have the option of manually assigning the queryParams to a public property within your component and connecting it to the queryParams property of the link.

Component:

  public queryParams;

  ngOnInit(){
    this.queryParams = this.getQueryParams();
  }

HTML:

<a [routerLink]="['/search/' + search.id]" [queryParams]="queryParams">Destination</a>

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

The outcome of the .then function is not defined following another .then function

I am struggling with handling async requests in Angular within Ionic4. I have successfully loaded a .json file and saved it into an array, but when trying to edit the array within a loop, my promise resolves before the loop finishes. As a newcomer to Angul ...

Ways to eliminate the top space in the background image

After implementing a basic HTML code to add a background image to my webpage, I noticed there is still some empty space at the top of the page. I attempted to adjust the position of the background image using the following code, but it only lifted it upwar ...

Updating $scope in AngularJS works in JavaScript, but the changes are not reflected in the HTML

After making a request to the server and receiving a correct response, I have an array of objects called $scope.photos. However, when attempting to add a new photo by sending a request and then trying two different methods to update the $scope in the HTML, ...

The webpage runs smoothly in the browser, but unfortunately displays a blank screen on mobile devices

I'm currently in the process of developing an app utilizing a combination of the Ionic Framework, Phonegap, and AngularJS. Within the app, I have a directive called Item. While this directive functions perfectly when testing the application on a brow ...

What is the best way to set a value for a specific column using JavaScript in a RadGrid?

One of my requirements involves having a Radgrid where all rows are always in edit mode. Specifically, I am looking to implement a functionality in one of the columns where after an item is edited, all rows in that column should take on the same value. Her ...

How to retrieve the current element in AngularJS version 1.x

I have done extensive research on how to get the current element in jQuery using $(this). However, I have not been able to find a similar method in AngularJS 1.x. Here is what I have tried: angular.element(this), but it does not work as expected. Below is ...

Swapping out the document.createElement() method for React.createElement()

In a JavaScript library, I utilize document.createElement() to create an HTMLElement. Now, I am looking to develop a React library with the same functionality. My initial thought was to substitute document.createElement() with React.createElement(). There ...

angular table disabled based on condition

I have a table in my HTML file and I am trying to figure out how to disable the onClick function if the start date is greater than the current date. <ng-container matColumnDef="d"> <th mat-header-cell ...

Traversing a nested array using jQuery

I'm attempting to utilize jQuery's each function in order to iterate through the provided array. My aim is to identify the key ("Name") and display its corresponding array values on the webpage. Array ( [E-mail] => Array ( ...

Exploring the latest updates in MUI modern version

The MUI documentation suggests using a modern folder with components designed for modern browsers. Is there a way to configure webpack to automatically rewrite imports like import {Box} from "@mui/material" to use the modern version without manually changi ...

JS causes the navigator to malfunction

UPDATE: Greetings! I have developed a navigation bar that loads various pages into a specific div. Here is the source code for the navigation bar: <div id="navBar"> <ul> <li><a href="#" onClick="document.getElementById('bott ...

(React) Material-UI: The controlled checked state of SwitchBase is being altered by a component, causing it to become uncontrolled

Utilizing the useState hook, I have an array that contains various conditions const [conditions, setConditions] = useState([]) Each element in the array is structured as {condition: "some string", value: boolean} My array is rendered in the following man ...

Determine the value of a field by utilizing the values of two other fields through an onChange event

Setting the Stage: Imagine a scenario with 3 key fields: quantity, singlePrice, and totalPrice. I want these fields to be part of my form, with totalPrice being dynamically recalculated whenever quantity or singlePrice changes. My Approach: I created ...

Using orchestrate.js for local passport.js authentication

I need assistance finding a tutorial or resources for setting up local passport.js authentication with Orchestrate as the user storage. Most of the resources I have come across are based on MongoDB, but our project requires us to use Orchestrate. Any advic ...

What made the "in" operator not the best choice in this situation?

When I set out to create a type that represents the values of a given object type, I initially came up with this: type Book = { name:string, year:number, author:string } // expected result "string" | "number" type ValueOf<T ex ...

Using node.js to send a response with response.writeHead on the http module

While working on my own custom http module, I stumbled upon a few confusing points while studying the official node.js http module api: When a user utilizes the response.writeHead(statusCode, [reasonPhrase], [headers]) function, are the headers suppose ...

Creating conditional statements in Angular 2 templates - the power of if, elseif

Can the if elseif else structure be used in an Angular 2 template? Here is an example of using if else: [text]="company ? company.name : 'Select a company'" I am looking to incorporate elseif into this. ...

Unable to fetch information from the controllerAPI function within the tsx file due to a Module Parse error

I am currently working on fetching records from a database using ControllerApi and displaying them through React. The code snippet below is from a file with a *.tsx extension: import React, { useState } from 'react'; import ReactDOM from 'r ...

How do I dynamically adjust class names for menu items as I scroll using jQuery?

I have a website with different sections identified by unique IDs, such as: <section id="home"> First Section </section> <section id="about_us"> Second Section </section> <section id="what_we_do&q ...

Emphasize a word in a Typescript text by wrapping it in an HTML tag

I've been experimenting with using HTML tags within TypeScript to highlight specific words in a sentence before displaying the sentence in HTML. Despite searching on StackOverflow for various solutions, I haven't been able to find one that works. ...