Amp html 如何在AMP中混合多个json数据源的结果?
我将输出一个链接列表,其中我需要在模板中混合来自两个单独JSON数据源的值,而不是一个 伪标记:Amp html 如何在AMP中混合多个json数据源的结果?,amp-html,Amp Html,我将输出一个链接列表,其中我需要在模板中混合来自两个单独JSON数据源的值,而不是一个 伪标记: <a href="{link1}/{link2}">{title1}</a> 在上面的例子中,link1和title1来自JSON#1,而link2来自JSON#2 需要明确的是:我知道通过将JSON数据源合并到单个服务中可以完全避免这一挑战,但在我的场景中,这是不可能的,原因与此无关 我将以上伪标记进一步扩展到amp列表中: <amp-list id="myli
<a href="{link1}/{link2}">{title1}</a>
在上面的例子中,link1和title1来自JSON#1,而link2来自JSON#2
需要明确的是:我知道通过将JSON数据源合并到单个服务中可以完全避免这一挑战,但在我的场景中,这是不可能的,原因与此无关
我将以上伪标记进一步扩展到amp列表中:
<amp-list id="mylist" width="auto" height="160px" layout="fixed-height" src="//json1">
<template type="amp-mustache">
<a href="{{buyURL}}={{UID}}">
<amp-img src="{{logoURL}}" width="{{logoWidth}}" height="{{logoHeight}}" layout="fixed" alt="{{name}}">
</amp-img>
</a>
</template>
</amp-list>
<amp-state id=json2 src=json2></amp-state>
<amp-list id="mylist" width="auto" height="160px" layout="fixed-height" src="//json1">
<template type="amp-mustache">
<a href="{{buyURL}}" [href]="'{{buyUrl}}=' + json2.uuids['{{name}}']" >
<amp-img src="{{logoURL}}" width="{{logoWidth}}" height="{{logoHeight}}" layout="fixed" alt="{{name}}">
</amp-img>
</a>
</template>
</amp-list>
我删除了与问题无关的属性。在上面的代码中,除了{{UID}一个变量之外,所有变量都来自json1。这一个需要来自一个单独的json服务,现在我们称之为json2
因此,我的挑战是集成来自第二个数据源的值,并将其混合到绑定到第一个数据源的模板输出中。以下是我尝试过的:
- 相互嵌套模板似乎是不允许的,它会抛出一个AMP错误
- 允许嵌套列表,但您只能将模板放在正确列表的层次结构中。将它们放在同一级别,AMP只需选择第一个或抛出错误“找不到模板”
我被困在实现上述场景中。我不知道如何接近它,也不知道它在哪里。您可以尝试将amp state和amp list结合起来:
<amp-list id="mylist" width="auto" height="160px" layout="fixed-height" src="//json1">
<template type="amp-mustache">
<a href="{{buyURL}}={{UID}}">
<amp-img src="{{logoURL}}" width="{{logoWidth}}" height="{{logoHeight}}" layout="fixed" alt="{{name}}">
</amp-img>
</a>
</template>
</amp-list>
<amp-state id=json2 src=json2></amp-state>
<amp-list id="mylist" width="auto" height="160px" layout="fixed-height" src="//json1">
<template type="amp-mustache">
<a href="{{buyURL}}" [href]="'{{buyUrl}}=' + json2.uuids['{{name}}']" >
<amp-img src="{{logoURL}}" width="{{logoWidth}}" height="{{logoHeight}}" layout="fixed" alt="{{name}}">
</amp-img>
</a>
</template>
</amp-list>
amp list当前在呈现其内容时评估绑定。然而,这种行为目前正在讨论中,可能会改变(通过适当的解决方法)
下面是一个演示该方法的示例: 自我回答我的问题。请注意,这个答案受到了塞巴斯蒂安·奔驰(Sebastian Benz)的另一个答案的巨大启发,但也有一些小改动
<amp-state id="requestID">
<script type="application/json">
{
"unid": "123654"
}
</script>
</amp-state>
<amp-list id="mylist" width="auto" height="160px" src="//.." items="..">
<template type="amp-mustache">
<a href="" [href]="'{{URL}}&id=' + requestID.unid">{{name}}</a>
</template>
</amp-list>
在上面的示例中,{{URL}}和{name}来自amp-list JSON源,而requestID.unid来自第二个JSON源。该语法显示了如何将两个数据源连接到单个属性:href。与给出的其他答案的主要区别是:
- href应为空,未分配{{URL}
- [href]中的表达式不应分配变量,而应计算返回值