简体   繁体   中英

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>

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
})">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM