繁体   English   中英

如何动态地将新行添加到以AMP状态存储的字符串?

[英]How to dynamically add a new line to the string stored in the AMP state?

我正在学习使用“加速的移动页面”。 我想在用户按下按钮时向存储在amp-state中的字符串中添加新行和一些文本内容。 所以我尝试了这个:

 <script src="https://cdn.ampproject.org/v0.js"></script> <script src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script> <amp-state id="formState"> <script type="application/json"> { "message": "Some text" } </script> </amp-state> <textarea [text]="formState.message"></textarea> <button type="button" on="tap:AMP.setState({formState: {message: formState.message + '\\nSome another text'} })"> Button </button> 

不幸的是,似乎它以某种方式转义了\\字符。 所以我从字面上得到了Some text\\nSome another texttextarea Some text\\nSome another text

我已经尝试过String.fromCharCode等,但是在AMP中被禁止...被卡住了。

我找不到有关此用例的太多文档,所以在这里问:有没有办法在用户交互中向存储在amp-state的字符串动态添加新行?

定义换行符:

<amp-state id="formState">
  <script type="application/json">
  {
    "message": "Some text",
    "newLine": "\n"
  }
  </script>
</amp-state>

然后:

 on="tap:AMP.setState({formState: {message: formState.message + formState.newLine + 'asdf'} })">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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