【问题标题】:How can I perform string manipulations inside an amp-list template?如何在 amp-list 模板中执行字符串操作?
【发布时间】:2017-09-20 02:08:07
【问题描述】:

我可以使用amp-bind 更新[href] 以获得锚点。但是,如果我在amp-list 模板中包含相同的锚点,则在应用模板时[href] 似乎会损坏(它由原始位置和URI 编码前置)。值得注意的是,字符串周围缺少单引号,导致表达式编译错误。

在以下示例中,amp-list 之前的链接在按下按钮时会正常更新,而 amp-list 模板生成的链接会被破坏。有趣的是,模板 in 中的锚仍然可以正常读取;只有在应用模板之后,[href] 才会被破坏。

在模板内锚定:

<a href="https://amp-test/" [href]="path ? 'https://amp-test/' + path.split(' ').join('-') : 'https://amp-test/'">{{linkName}}</a>

应用模板后的锚点:

<a href="https://amp-test/" [href]="https://amp-test/path%20?%20%27https://amp-test/%27%20+%20path.split(%27%20%27).join(%27-%27)%20:%20%27https://amp-test/%27" target="_top" class="i-amphtml-error">one path</a>

因此,可能伴随标题问题的一些问题包括:amp-bind 操作是否应该在 amp-list 内部工作?即我看到的是预期的行为还是错误?

最小示例 (jsfiddle)

results.json

{
    "items": [
        { "linkName": "one path" },
        { "linkName": "two path" }
    ]
}

index.html

<!doctype html>
<html ⚡>
<head>
    <title>amp-bind in amp-list</title>
    <meta charset="utf-8">
    <script async src="https://cdn.ampproject.org/v0.js"></script>
    <script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
    <script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>
    <script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.1.js"></script>
    <link rel="canonical" href="https://cmphys.com/">
    <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
    <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>

    <style amp-custom>
    </style>
</head>
<body>
    <button on="tap:AMP.setState({path: 'relative path'})">Update</button>

    <hr>
    <p>
        <a href="https://amp-test/" [href]="path ? 'https://amp-test/' + path.split(' ').join('-') : 'https://amp-test/'">link</a>
    </p>

    <hr>
    <amp-list id="myList" class="list" layout="fixed" width="200" height="100" src="/results.json">
        <template type="amp-mustache">
            <a href="https://amp-test/" [href]="path ? 'https://amp-test/' + path.split(' ').join('-') : 'https://amp-test/'">{{linkName}}</a><br>
        </template>
    </amp-list>

</body>
</html>

【问题讨论】:

    标签: amp-html


    【解决方案1】:

    尝试使用Sanitizers 来操纵您帖子的 AMP 内容。为此,您应避免使用会导致 AMP 验证器失败的禁止 URL 协议或表达式。

    关于 amp-list 中的 amp-bind 操作,您可能需要检查此 documentation,其中提到允许绑定到 &lt;amp-list&gt; 组件和属性,其行为是:

    如果表达式是字符串,则从字符串 URL 获取并呈现 JSON。如果表达式是对象或数组,则呈现表达式数据。

    【讨论】:

    • 我不知道您为什么建议使用消毒;我从 AMP HTML 文档开始,而不是将页面从 HTML 转换为 AMP HTML。您指向(并引用)的文档仅指 amp-list 属性 [src],而不是 amp-list 模板中的元素属性。
    猜你喜欢
    • 2013-05-07
    • 2011-08-10
    • 2016-06-09
    • 2011-06-17
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    相关资源
    最近更新 更多