AMP
  • 電子郵件

進階伺服器請求

簡介

此範例示範如何在電子郵件中顯示來自伺服器的資料,並在考量使用者輸入的情況下隨後重新整理資料。

由於 AMP for Email 不允許繫結至 amp-list 中的 [src],因此結合了 amp-listamp-form,兩者共用相同的 amp-mustache 範本。當首次提交表單時,amp-list 會隱藏,表單的回應會取而代之。

實作

定義範本並給予 id,以便能從 amp-listamp-form 使用。

<template id="animal-template" type="amp-mustache">
  <p>This displays data sent from a server.</p>
  {{#animal}}
  <p>You have selected {{animal}}.</p>
  {{/animal}}
</template>

當使用者變更選取項目時,變更事件會觸發表單提交並隱藏 amp-liston="change:animal-form.submit,animal-list.hide"

amp-form 使用與 amp-list 相同的範本來呈現伺服器回應,使從 amp-list 切換到 amp-form 看起來無縫接軌。

使用預留位置,使載入狀態看起來與表單的提交狀態相同。

選取一種動物以更新伺服器回應。

載入中...
載入中...
<form id="animal-form" method="post" action-xhr="https://amp.dev.org.tw/documentation/examples/api/echo">
  <div>
    <p>Select an animal to update the server response.</p>
    <select name="animal" on="change:animal-form.submit,animal-list.hide">
      <option value="dog">Dog</option>
      <option value="cat">Cat</option>
      <option value="parrot">Parrot</option>
    </select>
  </div>
  <div submitting>Loading ...</div>
  <div submit-success template="animal-template"></div>
</form>
<amp-list id="animal-list" max-items="1" template="animal-template" src="https://amp.dev.org.tw/static/inline-examples/data/amp-list-animals.json" layout="fixed-height" height="85">
  <div placeholder>Loading ...</div>
</amp-list>
需要進一步說明嗎?

如果此頁面上的說明未涵蓋您的所有問題,請隨時與其他 AMP 使用者聯繫,討論您的確切使用案例。

前往 Stack Overflow
有未說明的特色功能?

AMP 專案強烈鼓勵您的參與和貢獻!我們希望您能成為我們開放原始碼社群的長期參與者,但我們也歡迎您針對您特別關注的問題做出一次性貢獻。

在 GitHub 上編輯範例