在 action xhr 中传递 AMP 状态

Pass AMP state in action xhr

我正在研究 amp-form。在 action-xhr 中,我必须通过 url.

的放大器状态

leadProfileId 是放大器状态。

<form method="post" action-xhr='@currentHostUrl/api/stocks/leadProfileId/negotiate' id="submitOfferForm" target="_top"
                  on="submit-success: AMP.setState({ selectedOffer : null })">
                <input hidden name="priceOffered" [value]="selectedOffer">
                <input hidden name="mobileNumber" [value]="mobileNo">
            </form>

我尝试了以下方法,但没有用。

  1. '@currentHostUrl/api/stocks/{{leadProfileId}}/negotiate'
  2. "'@currentHostUrl/api/stocks/'leadProfileId'/协商'"

Jay Gray 正确地询问了您在哪里拥有 amp-state 标签?使用 AMP.setState({ anyData: 'anyValue'})

不清楚在哪里写入数据

不幸的是,我不知道如何从 amp-state 绑定 action-xhr 属性。请参阅此主题:https://github.com/ampproject/amphtml/issues/11222

当我们不知道如何在 AMP 中绑定某些内容时,amp-list 组件通常可以帮助我们。解决方案:

<!--
     This is the minimum valid AMP HTML document. Type away
     here and the AMP Validator will re-check your document on the fly.
-->
<!DOCTYPE html>
<html ⚡>

<head>
  <meta charset="utf-8" />
  <link rel="canonical" href="self.html" />
  <meta name="viewport" content="width=device-width,minimum-scale=1" />
  <style amp-boilerplate>
    body {
      -webkit-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
      -moz-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
      -ms-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
      animation: -amp-start 8s steps(1, end) 0s 1 normal both;
    }
    
    @-webkit-keyframes -amp-start {
      from {
        visibility: hidden;
      }
      to {
        visibility: visible;
      }
    }
    
    @-moz-keyframes -amp-start {
      from {
        visibility: hidden;
      }
      to {
        visibility: visible;
      }
    }
    
    @-ms-keyframes -amp-start {
      from {
        visibility: hidden;
      }
      to {
        visibility: visible;
      }
    }
    
    @-o-keyframes -amp-start {
      from {
        visibility: hidden;
      }
      to {
        visibility: visible;
      }
    }
    
    @keyframes -amp-start {
      from {
        visibility: hidden;
      }
      to {
        visibility: visible;
      }
    }
  </style>
  <noscript>
    <style amp-boilerplate>
      body {
        -webkit-animation: none;
        -moz-animation: none;
        -ms-animation: none;
        animation: none;
      }
    </style>
  </noscript>
  <script async src="https://cdn.ampproject.org/v0.js"></script>
  <script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
  <script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
  <script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>
  <script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
</head>

<body>
  <amp-state id="localState">
    <script type="application/json">
      {
        "leadProfileId": "api"
      }
    </script>
  </amp-state>

  <h2>AMP-Form</h2>

  <amp-list width="auto" height="100" items="." src="amp-state:localState" single-item>
    <template type="amp-mustache">

      <form method="post" target="_top" action-xhr="https://amp.dev/documentation/examples/{{leadProfileId}}/verify-form-input-text-xhr/">
        <button type="submit">Submit</button>
        <div submit-success>Form send successful!</div>
        <div submit-error>Form send failed!</div>
      </form>

    </template>
  </amp-list>

</body>

</html>

Codepen: https://codepen.io/alexandr-kazakov/pen/LYNWYzY?editors=1000