0

I have a component where I have a list of objects and I want to pass in a single input parameter that describes the route that should be navigated to when any specific row is clicked.

Something like the below:

    //Some sort of object may have an 'id' field in some cases (but not in other cases... if it didn't I would pass in a different string below)
    @Input()
    listData: any[]; 
    @Input()
    link: string; // Input of something like "/List/Detail/${row.id}"

    rowClick(row: any){
      //Grab row.id and interpolate it so result is "/List/Detail/1"
      ???
      router.navigate([url]);
    }

Seems like it should be simple, but I'm having a hard time wrapping my head around it.

The idea here is that my list items will sometimes link to one page and sometimes link to a different page depending on the 'link' parameter.

5
  • 1
    You could use lodash _.template lodash.com/docs/4.17.15#template Commented Dec 12, 2019 at 22:30
  • const url = ${row.url}/${row.id}; Commented Dec 12, 2019 at 22:31
  • @ChristopherMarshall you must remove the ${row.id} part btw Commented Dec 12, 2019 at 22:32
  • I'm not really sure the structure of the Obj so just put the example in a string literal. Commented Dec 12, 2019 at 22:34
  • I already have lodash in the project so I'll give that a try. Looks like it should work. Thanks! Commented Dec 12, 2019 at 22:35

1 Answer 1

1

Using lodash _.template:

const compiledTemplate = _.template(this.link);

compiledTemplate({ row });

Using Javascript:

const linkParts = this.link.split('/');

linkParts.pop();
linkParts.push(row.id);
const linkWithRowId = linkParts.join('/');
Sign up to request clarification or add additional context in comments.

Comments

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.