Trouble with Syntax in Angular App While Declaring a Variable

Managing pagination in my Angular application has been a smooth process with the function I have implemented. It involves subscribing to URL parameters and then using the router to navigate based on those parameters, including the page number passed as a value. One parameter indicates whether the filter is active, while the other holds the filter value(s).

Here is the original implementation:

public paginate(page) {
  this.route.params.subscribe(
    (params: any) => {
      this.pn_location_e = params['pn_location_e'];
      this.pn_location_v = params['pn_location_v'];
    }
  );

  this.router.navigate(
    ['/clients', {
      page: page,
      pn_location_e: this.pn_location_e,
      pn_location_v: this.pn_location_v,
    }]);

  let fn = resRecordsData => {
    this.records = resRecordsData;
    let data = resRecordsData.data;
  };

  this.dataService.filterByInput(
    page - 1, this.pagesize, this.location, fn);
}

Everything was functioning smoothly following this setup.

However, a recent change by a colleague switched the filter syntax from "_" to ".". So, the line changed from:

this.pn_location_e = params['pn_location_e'];

to:

this.pn_location.e = params['pn_location.e'];

The challenge now is that initializing the variable with this new syntax in my Angular component results in a syntax error. Attempting pn_location.e throws an error, and even trying pn_location['.e'] does not work either.

Is there a workaround for this issue, or should we revert back to using the underscore syntax for our filter parameters?

Answer №1

Enclosing the property names in quotes enables the assignments:

function displayPage(page) {
  this.route.params.subscribe(
    (params: any) => {
      this.pn_location_e = params['pn_location.e'];
      this.pn_location_v = params['pn_location.v'];
    }
  );

  this.router.navigate(
    ['/clients', {
      page: page,
      'pn_location.e': this.pn_location_e,
      'pn_location.v': this.pn_location_v,
    }]);
}

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

JavaScript error type mismatch

Currently, I am utilizing select2.js to highlight a specific li element that is received as a response. var cityCounter = -1; var sul = $('.select2-search-choice'); $(".select2-input").keyup(function (e) { // TODO CODE // console.log($( ...

Refresh an iframe smoothly and without any visual distraction (using JavaScript)

Does anyone have a clever solution to dynamically refresh an iframe without the annoying flickering or flashing that usually occurs when the page reloads? Is it possible to incorporate a smooth blur-out and blur-in animation instead of the unappealing flic ...

Disable the border and background colors in CloudFlare Turnstile

I have implemented the CloudFlare reCaptcha Turnstile and I am looking to modify the widget by removing the border and background color. I believe this customization can be achieved using CSS or Javascript. Thank you for any assistance! ...

Tips for enhancing the contents of a single card within a react-bootstrap accordion

Currently, I am facing an issue with my columns expanding all cards at once when utilizing react-bootstrap accordion. My goal is to have each card expand individually upon clicking on its respective link. However, I am encountering difficulties in implem ...

Observing the evolution of Vue with Observable notifications

I'm currently working on creating a method to verify a user's login status using firebase's firebase.auth().onAuthStateChanged(). My intention is to make this functionality accessible throughout my app as a global variable. Presently, I am ...

Using Vue to bring in an npm module that does not have any exports

I'm facing a challenge when trying to bring in an npm package into a Vue component. This package (JSPrintManager - here) consists of a JavaScript file with no exports. Therefore, I'm unable to utilize the following import statements: import { J ...

Unable to choose options fetched from the API in Select2 Ajax

I've been struggling to populate a select input with data from my API using Select2. Despite having everything set up correctly, I'm facing an issue where I can't select anything once the results are displayed. I've been working on this ...

A Guide to Catching Targeted React Notifications in Sentry using Next.js

In my Next.js application, I have implemented Sentry for error tracking. While I have successfully set up Sentry to capture errors within my try/catch blocks, I am currently struggling with capturing specific errors and warnings at a global level in my sen ...

It appears that Promise.all is not adequately ensuring that all tasks are completed before moving on

In my current project, I am trying to achieve a complex cycle where an HTTP GET request is executed to fetch data, followed by the creation of multiple "subrequests" based on that data. The goal is to ensure that the next iteration of the cycle begins only ...

What are the steps to utilize my JavaScript class within NodeJS?

Within a JavaScript file, I implemented a class called 'engine' that has a constructor and a method: class engine { constructor(a) { this._a = a; } foo = function() { console.log(this._a); } } module.exports.engine ...

What is causing the slow performance of this JavaScript array retrieval?

I am working with a large array called frames_to_boxes, consisting of 5000 elements. Each element is an array of Objects belonging to the Box class: class Box { constructor(x, y, width, height, frame, object_class, id) { this.x = x; this.y = y; ...

What is the alternative method for passing a function from one component to another without using an Arrow Function?

In the code snippet below, I encountered an issue while attempting to pass the 'handleChange' method to another component called 'TodoList'. Despite trying to bind the method, the problem persisted. import React from "react"; ...

Obtain window dimensions using Angular when the page loads

To determine the window size upon resizing, I utilize the following code: @HostListener('window:resize', ['$event']) resizeHandler(event: any) { this.isSmallScreen = event.target.innerWidth > 600; } Is there a way to determine ...

Is there a one-liner to efficiently eliminate all instances of a specific sub-string from an array using the filter

In search of a solution to filter an array and eliminate all instances of substrings, similar to removing entire strings as shown below: const x = ["don't delete", "delete", "delete", "don't delete", "delete", "don't delete"] x= x.filter(i ...

Livereload in Gulp fails to automatically restart the server

How can I make my gulp+livereload server automatically restart and update the page when JS files are changed? Below is a snippet from my gulpfile.js: var gulp = require('gulp'), livereload = require('gulp-livereload'), ...

Loading React router on every route page

<BrowserRouter> <div> <Route path={"/"} component={Home} /> <Route path={"/component"} component={AnotherComp} /> <Route path={"*"} component={NotFound} /> </div> </ ...

What is the best approach for looping through a JSON object?

Currently constructing a project using Angular and incorporating redux. In my JSON object, there are nested objects with specific values. Let's imagine: name: "john", sex: "m" children: [ { name: "joe", sex: "m" children: [ { name: " ...

Is jQuery capable of appropriately escaping my quotes?

Currently, I am utilizing $.cookie() to retrieve all the values from a cookie which are stored in JSON format: var properties = $.cookie('params'); The output of properties is: {"distinct_id": "13f97d6600b42e-000e6293c-6b1b2e75-232800-13f97d66 ...

Having trouble displaying a pre-designed 3D model in three.js

I would greatly appreciate it if someone could provide an example and elaborate on how to render a 3D object using three.js or similar libraries. The model already exists, so the focus is solely on rendering it effectively. Can you please guide me throug ...

Absence of property persists despite the use of null coalescing and optional chaining

Having some trouble with a piece of code that utilizes optional chaining and null coalescing. Despite this, I am confused as to why it is still flagging an error about the property not existing. See image below for more details: The error message display ...