首先,我了解如何在amp-html中setState
使用amp-bind,如下所示:
<amp-selector on="select:AMP.setState({ guess: event.targetOption })">
<div class="button" option="a">Answer 1</div>
<div class="button" option="b">Answer 2</div>
<div class="button" option="c">Answer 3</div>
</amp-selector>
现在说我选择按钮Answer 2
,然后printState()
上的状态将显示为{"guess":"b"}
,此div将不再被隐藏:
<div hidden [hidden]="!guess">
<h4>You have the correct answer!</h4>
</div>
我的问题是:一旦隐藏的div变得可见,如何设置另一个键/值?换句话说,我怎样才能让它发挥作用?
<div hidden [hidden]="!guess" on="WhenThisIsVisible:AMP.setState({"text":"hi!"})"></div>
我可以使用tap:
代替我的假装活动,但我希望用户不必点击div setState
。
一旦div显示,printState()
将返回{"guess":"b","text":"hi!"}
答案 0 :(得分:0)
您是否可以在div中使用span
,然后使用以下内容:
<span [text]="text"></span>
以下是完整的样本:
<!doctype html>
<html ⚡>
<head>
<meta charset="utf-8">
<title>My AMP Page</title>
<link rel="canonical" href="self.html" />
<meta name="viewport" content="width=device-width,minimum-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>
<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-selector" src="https://cdn.ampproject.org/v0/amp-selector-0.1.js"></script>
<style amp-custom>
h1 {
margin: 1rem;
}
</style>
</head>
<body>
<amp-selector on="select:AMP.setState({ guess: event.targetOption, text: 'hi' })">
<div class="button" option="a">Answer 1</div>
<div class="button" option="b">Answer 2</div>
<div class="button" option="c">Answer 3</div>
</amp-selector>
<div hidden [hidden]="!guess">
<span [text]="text"></span>
<h4>You have the correct answer!</h4>
</div>
</body>
</html>