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

问题描述 投票:0回答:1

我正在学习使用Accelerated Mobile Pages。我想在用户按下按钮时将新行和一些文本内容添加到存储在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 text得到了textarea

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

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

javascript html amp-html accelerated-mobile-page google-amp
1个回答
2
投票

定义换行符:

<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'} })">
© www.soinside.com 2019 - 2024. All rights reserved.