1

I have a function that I use to handle pagination in my Angular app. It was working as expected - I subscribe to the url params and then use the router to navigate according to those params while taking in the page number as a passed in value. One param is a boolean indicating whether the filter is currently active, and the second param is the value(s) itself for the filter.

This is the working version:

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 above was working as expected.

However, recently a colleague changed the filter syntax from using an "_" to using a ".". So it went from this:

this.pn_location_e = params['pn_location_e'];

to this:

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

The problem is, in my Angular component I can't initialize the variable with that syntax. When I try and initialize like this:

pn_location.e

... I get a syntax error. I also tried doing this pn_location['.e'], but that also won't work (also causes a syntax error).

Is there a way around this? Or do we just need to go back to using the underscore syntax for our filters params?

1
  • Can you provide the exact syntax error that you are getting? Commented Apr 19, 2018 at 22:36

1 Answer 1

2

Surrounding the property names with quotes will allow the assignments:

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,
    }]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Thanks @ConnorsFan!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.