首页 > 解决方案 > ngFor,将输入与 Aloglia Places 功能链接

问题描述

我正在使用 Angular8 在 ngFor 中设置 Algolia Places 输入。当我使用(change)时,它在我第二次在输入中写入时工作正常。这很好,但不完全是我想要的。

我认为函数和输入之间的链接不是在创建输入(逻辑)时自动建立的。我想要的是成功地将输入与函数链接起来。我应该怎么做?

HTML部分:

<ng-container *ngFor="let place of places; let i = index;">
<input id="{{ 'inputaddress' + i}}" (change)="addressSearchIndex(i)" type="text" class="form-control" />
</ng-container>

Component.ts 部分:

addressSearchIndex(index) {
        this.placesAutocompleteAddress = placesAlgolia({
            appId: <ID>,
            apiKey: <Key>,
            container: document.querySelector('#inputaddress'+index),
            templates: {
                value: function(suggestion) {
                    return suggestion.name;
                }
            }
        }).configure({
            type: 'address'
        });
        this.placesAutocompleteAddress.on('change', function resultSelected(e) {
            this.lat = e.suggestion.latlng["lat"];
            this.lng = e.suggestion.latlng["lng"];
        });
    }

标签: angulartypescriptngforalgolia

解决方案


尝试 keypress 或 keydown 事件而不是更改


推荐阅读