我正在学习使用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
中,用户交互?
定义换行符:
<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'} })">