Ronnie
Ronnie

Reputation: 11198

How to escape UI router params in ui-sref

I have a list of clients and I want to pass the client name using $stateParams

<a ui-sref="cmd.projects_by_client_id({
  clientId:{{client.vid[0].value}},
  client:'{{client.title[0].value}}' <-- the issue
})">
  {{client.title[0].value}}
</a>

What I have there works for the most part. My issue is if the client name contains a single quote, I get a lexer error complaining about an unclosed single quote, obviously.

I am trying to escape the stateParam. I've tried passing the string through a function but the function does not seem to get called. Probably because it is not supported through ui-sref

<a ui-sref="cmd.projects_by_client_id({
  clientId:{{client.vid[0].value}},
  client:$scope.someUrlEncodeFunction('{{client.title[0].value}}')
})">
  {{client.title[0].value}}
</a>

I've search around but I haven't been able to find an answer to this problem.

EDIT

I found this works because it is still in JSON format, but I'd like to not have to parse it again:

<a ui-sref="cmd.projects_by_client_id({
  clientId:{{client.vid[0].value}},
  client:{{client.title[0]}}
})">
  {{client.title[0].value}}
</a>

Upvotes: 1

Views: 250

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

You shouldn't be using {{}} there & no need to wrap it with single quotes. client.title[0].value will get evaluated against current scope & href will get created based on that value.

<a ui-sref="cmd.projects_by_client_id({
  clientId: client.vid[0].value,
  client: client.title[0].value
})">

Upvotes: 1

Related Questions