rajat44
rajat44

Reputation: 5121

How to append state variables' value into a string in AMP pages?

I have an AMP-state :

<amp-state id="Form">
  <script type="application/json">
    {
    "id" :"1",
    }
  </script>
</amp-state>

I want to dynamically make the redirect URL with the value of id, and I am doing like this:

<a [href] = "myWebsite.com/{{Form.id}}" href="#" />

Sample URL: myWebsite.com/1002

But instead of getting the value of the state variable id, I am getting:

myWebiste.com/%7B%7BForm.id%7D%7D

Upvotes: 1

Views: 1296

Answers (2)

Chris Aby Antony
Chris Aby Antony

Reputation: 813

I believe the following will fix your problem :

<a [href] = " 'myWebsite.com/' + Form.id " href="#" />

you don't need the braces here.

Upvotes: 2

abielita
abielita

Reputation: 13469

Based from this link, at the moment it's only possible to set the values for global variables via AMP.setState but not for variables declared inside amp-state.

Example: AMP.setState(foo=123) -> is working

AMP.setState(myAmpState.foo=123) where

<amp-state id="myAmpState">
      <script type="application/json">
      {
          "foo": "234"
      }
.....

is not working.

Current workaround is setting default variables.

Upvotes: 0

Related Questions