javascript – 如何使用顶级数组中的amp-selector设置对象以在amp-bind中使用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 如何使用顶级数组中的amp-selector设置对象以在amp-bind中使用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2501字,纯文字阅读大概需要4分钟。
内容图文
![javascript – 如何使用顶级数组中的amp-selector设置对象以在amp-bind中使用](/upload/InfoBanner/zyjiaocheng/789/00291c9025f34a1f9225834ad0b87640.jpg)
在autosuggest example之后,我需要保留并使用对象而不是来自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"
}]
使用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>
感谢this answer for {{.}}语法,这在Mustache docs中是无处可去的.但是,amp-bind中locationObj的绑定字段打印[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"
/>
AMP Docs没有说明在控制台中记录任何内容的任何方法,所以我知道在locationObj中通过{{.}}设置了什么
解决方法:
感谢Carlos在Amp Google Forum.正确的方法来保存和访问< 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>
在< amp-state>中定义相同的[src]在< amp-list>中将响应存储在状态变量中,以后可以用来根据对象的唯一成员获取项目(例如allLocations.filter(x => x.code == event.targetOption)[0] case)来自本地保存的状态数组.
内容总结
以上是互联网集市为您收集整理的javascript – 如何使用顶级数组中的amp-selector设置对象以在amp-bind中使用全部内容,希望文章能够帮你解决javascript – 如何使用顶级数组中的amp-selector设置对象以在amp-bind中使用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。