【发布时间】:2019-07-04 07:36:57
【问题描述】:
我正在构建 amp 页面,该页面应根据国家/地区显示不同的下载 url。通过将isEU 组添加到<amp-geo> 配置,我可以通过css 显示/隐藏正确的<a> 元素。但我想要的只是页面上一个带有可变href的链接。我试图通过使用来实现这一点,但我无法使用<amp-state>,尽管它是正确生成的。
在我点击<button on="tap:AMP.setState({foo: 'amp-bind'})">Say "Hello amp-bind"</button> 之前,即使是 amp 文档中的示例也不起作用。然后它触发计算并且页面显示所有值。在此之前没有插值/计算值。
我在这里做错了什么?
<!doctype html>
<html ⚡>
<head>
<meta charset="utf-8">
<title>TESTPAGE</title>
<meta name="viewport"
content="width=device-width,minimum-scale=1,initial-scale=1">
<script async
src="https://cdn.ampproject.org/v0.js"></script>
<script async
custom-element="amp-geo"
src="https://cdn.ampproject.org/v0/amp-geo-0.1.js"></script>
<script async
custom-element="amp-bind"
src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
<style>
[class*=-only] {
display: none;
}
.amp-geo-group-isEU .eu-only {
display: block;
}
body:not(.amp-geo-group-isEU) .non-eu-only {
display: block;
}
</style>
</head>
<body class="amp-geo-pending">
<amp-state id="stateTest"
class="i-amphtml-element i-amphtml-layout-container"
i-amphtml-layout="container"
hidden=""
aria-hidden="true">
<script type="application/json">
{
"testInitialKey": "initial state value"
}
</script>
</amp-state>
<amp-state id="myCircle"
class="i-amphtml-element i-amphtml-layout-container"
i-amphtml-layout="container"
hidden=""
aria-hidden="true">
<script type="application/json">
{
"radius": "4"
}
</script>
</amp-state>
<amp-geo layout="nodisplay">
<script type="application/json">
{
"AmpBind": true,
"ISOCountryGroups": {
"isEU": ["at", "be", "bg", "bs", "ch", "cy", "cz", "de", "dk", "ee", "es", "fi", "fr", "gb", "gr", "hr", "hu", "ie", "is", "it", "li", "lt", "lu", "lv", "mt", "nl", "no", "pl", "pt", "ro", "se", "si", "sk"]
}
}
</script>
</amp-geo>
<h1 [class]="ampGeo.isEU ? 'isEU' : 'nonEU'"
[text]="'isEU: ' + (ampGeo.isEU ? 'true' : 'false').toUpperCase()"></h1>
<h2 [text]="stateTest.testInitialKey"></h2>
<a class="eu-only"
[href]="'http://google.com/' + ampGeo.ISOCountry == 'cy' ? 'cyprusLink' : 'defaultLink'">LINK_CY</a>
<a class="non-eu-only"
href="http://google.com/?frenchLink">LINK_FR</a>
<hr>
<amp-bind-macro id="circleArea"
arguments="radius"
expression="3.14 * radius * radius"></amp-bind-macro>
<div>
The circle has an area of <span [text]="circleArea(myCircle.radius)">0</span>.
</div>
<hr>
<p [text]="'Hello ' + foo">Hello World</p>
<button on="tap:AMP.setState({foo: 'amp-bind'})">Say "Hello amp-bind"</button>
</body>
</html>
【问题讨论】:
标签: javascript html amp-html