编程语言
首页 > 编程语言> > javascript – 如何使用顶级数组中的amp-selector设置对象以在amp-bind中使用

javascript – 如何使用顶级数组中的amp-selector设置对象以在amp-bind中使用

作者:互联网

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,mustache,amp-html
来源: https://codeday.me/bug/20190627/1305963.html