adn*_*fch 6 javascript mustache amp-html
按照autosuggest 示例,我需要保留和使用 object 而不是来自amp-selector.
这是来自 API 的 JSON:
[{
"name": "Singapore",
"cityCode": "SIN",
"countryName": "Singapore",
"type": "city"
}, {
"name": "Sinop",
"cityCode": "NOP",
"countryName": "Turkey",
"type": "city"
}, {
"name": "Sinop",
"cityCode": "OPS",
"countryName": "Brazil",
"type": "city"
}]
Run Code Online (Sandbox Code Playgroud)
使用 AMP 渲染:
<amp-list
class="autosuggest-box"
layout="fixed-height"
height="130"
src="<url>"
id="autosuggest-list"
single-item
items="."
>
<template type="amp-mustache">
<amp-selector
id="autosuggest-selector"
keyboard-select-mode="focus"
layout="container"
on="
select:
AMP.setState({
locationObj: event.targetOption,
showDropdown: false
}),
autosuggest-list.hide"
>
{{#.}}
<div
class="location-item no-outline"
role="option"
tabindex="0"
on="tap:autosuggest-list.hide"
option="{{.}}"
>{{name}}, {{countryName}}</div>
{{/.}}
</amp-selector>
</template>
</amp-list>
Run Code Online (Sandbox Code Playgroud)
感谢这个答案的{{.}}语法是无处小胡子文档。但是,locationObj在 amp-bind 打印的绑定字段[object Object]以及当我尝试使用locationObj.name它时打印null
这是绑定代码
<input
type="text"
class="search-box"
on="
input-debounced:
AMP.setState({
showDropdown: event.value
}),
autosuggest-list.show;
tap:
AMP.setState({
showDropdown: 'true'
}),
autosuggest-list.show"
[value]="locationObj ? locationObj.name : ''"
value=""
required
autocomplete="off"
/>
Run Code Online (Sandbox Code Playgroud)
AMP Docs 没有说明在控制台中记录任何内容的任何方式,所以我知道正在locationObj通过什么设置{{.}}
感谢Amp Google 论坛的 Carlos 。保存和访问响应的正确方法<amp-list>是通过<amp-state>.
<amp-list class="autosuggest-box"
layout="fixed-height"
height="130"
src="http://url.returning.json.array.com?query="
[src]="'http://url.returning.json.array.com?query=' + query"
id="autosuggest-list"
single-item
items=".">
<template type="amp-mustache">
<amp-selector
id="autosuggest-selector"
keyboard-select-mode="focus"
layout="container"
on="
select:
AMP.setState({
locationObj: allLocations.filter(x=>x.code == event.targetOption)[0],
showDropdown: false
}),
autosuggest-list.hide"
>
{{#.}}
<div
class="location-item no-outline"
role="option"
tabindex="0"
on="tap:autosuggest-list.hide"
option="{{code}}"
>{{name}}, {{countryName}}</div>
{{/.}}
</amp-selector>
</template>
</amp-list>
<amp-state
id="allLocations"
src="http://url.returning.json.array.com?query="
[src]="'http://url.returning.json.array.com?query=' + query"
></amp-state>
Run Code Online (Sandbox Code Playgroud)
定义相同的[src]in<amp-state>也会<amp-list>将响应存储在状态变量中,稍后可以使用状态变量allLocations.filter(x=>x.code == event.targetOption)[0]从本地保存的数组中获取基于对象的唯一成员(例如在本例中)的项目。
| 归档时间: |
|
| 查看次数: |
1584 次 |
| 最近记录: |