如何为 AMP 状态中存储的字符串动态添加新行?
How to dynamically add a new line to the string stored in the AMP state?
我正在学习使用 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>
不幸的是,它似乎以某种方式转义了 \
字符。所以我确实在 textarea
.
中得到了 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'} })">
我正在学习使用 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>
不幸的是,它似乎以某种方式转义了 \
字符。所以我确实在 textarea
.
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'} })">