如何在Google amp脚本中应用逻辑运算符?

问题描述 投票:1回答:1

关于Google AMP,我有以下代码:

<a [href]="'?view=amp&constraint=' + selectedOption1 + '&sort_by=' + selectedSorting"> Submit </a>

仅当'?view=amp&constraint='的值存在时,我才想显示selectedOption1

我怎么能做到相同?

我已经尝试过以下方法:

<a [href]="'?view=amp&amp;constraint=' + (selectedOption1 || '') + '&amp;sort_by=' + (selectedSorting || '')" > </a>
amp-html amp-bind
1个回答
2
投票

您必须对selectedOption1进行条件运算。这是一个共享的示例,其中有两个按钮供您参考,它们将更改selectedOption1的状态,因此您可以检查锚标记的href值,为了快速查看,我使用了段落标记以在段落中显示与href相同的值。希望这会对您有所帮助。

要运行此代码,请复制整个代码并将其替换为here上的正文部分AMP操场,只需确保已在amp操场示例页面上导入了amp-bind js。

<p [text]="selectedOption1 ? 'https://www.example.com?view=amp&constraint=' + selectedOption1 + '&sort_by=' + selectedSorting : 'www.example.com'">

</p>
  <a href="#" [href]="selectedOption1 ? 'https://www.example.com?view=amp&constraint=' + selectedOption1 + '&sort_by=' + selectedSorting : 'https://www.example.com'">
  Link
</a>
 </br>

<button on="tap:AMP.setState({selectedOption1: 'Interactivity',selectedSorting:'orderby_date'})">
  Say "Hello Interactivity"
</button>
<button on="tap:AMP.setState({selectedOption1: '',selectedSorting:''})">
  Say "Blank"
</button>
© www.soinside.com 2019 - 2024. All rights reserved.