高级服务器请求
简介
此示例演示如何在电子邮件中显示来自服务器的数据,并在考虑用户输入的情况下随后刷新它。
由于 AMP for Email 不允许绑定到 amp-list
中的 [src]
,因此它使用了 amp-list
和 amp-form
的组合,它们共享同一个 amp-mustache
模板。当第一次提交表单时,amp-list
被隐藏,表单的响应取而代之。
实现
定义一个模板并为其提供一个 id
,以便允许它从 amp-list
和 amp-form
中使用。
这会显示从服务器发送的数据。
{{#animal}}你选择了 {{animal}}。
{{/animal}}<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-list
:on="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.org.cn/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.org.cn/static/inline-examples/data/amp-list-animals.json" layout="fixed-height" height="85">
<div placeholder>Loading ...</div>
</amp-list>
需要进一步解释?
如果此页面上的说明未涵盖你的所有问题,请随时联系其他 AMP 用户以讨论你的确切用例。
前往 Stack Overflow 未解释的功能?AMP 项目强烈鼓励你的参与和贡献!我们希望你成为我们开源社区的持续参与者,但我们也欢迎你对你特别感兴趣的问题进行一次性贡献。
在 GitHub 上编辑示例-
作者: @fstanis