Angularjs应用程序使用autocomplete.js并从多个Algolia源读取不起作用

时间:2017-04-07 18:35:10

标签: javascript angularjs autocomplete algolia

我正在尝试使用AngularJS和Algolia的Autocomplete.js实现自动完成功能,其中包含来自Algolia的两个来源(索引)。

第二个源自动完成工作正常,但地址自动完成仅适用于键入的第一个字母/数字。当您键入第二个字符时,地址列表将消失。如果我交换这两个来源,地址工作正常,并且邮政编码仅适用于第一个字母/数字。

此外,在进行搜索时,console.log(address);会显示正在搜索的正确地址,但它不会显示在自动填充结果中。

这是一个实际的GIF: http://d.pr/i/2GCE

$scope.getDatasets = function() {
return [{
  source: algolia.sources.hits(settings.replica, { hitsPerPage: 5 }),
  displayKey: 'address',
  name: 'address',
  templates: {
    suggestion: function(suggestion) {

       var address = '<span class="address">' + suggestion._highlightResult.address.value;

       if(suggestion.aptmnt) {
        address += ', Unit ' +suggestion.aptmnt + '</span> | ';
       } else {
        address +='</span> | '; 
       }

       address += '<span>' + suggestion.totbdrms + ' beds </span> | ' +
        '<span>' + suggestion.totbaths + ' bath asdf </span> | ' +
        ' <span> $' + cleanPrice(suggestion.price) + '</span>';
        console.log(address);
        return address;
    }
  }
},{
  source: algolia.sources.hits(settings.zipcode, { hitsPerPage: 5 }),
  displayKey: 'zipcode',
  name: 'zipcode',
  templates: {
    suggestion: function(suggestion) {
       var zipcode = '<span class="zipcode">' + suggestion.zipcode + '</span>';
        return zipcode;
    }
  }
}]
};

我正在使用

jquery - v1.12.4
angularjs - 1.6.1
algoliasearch.angular.min.js - v3
algoliasearch.helper.min.js - v2.18.0
autocomplete.angular.min.js - v0.28.1
angular-ui-router.js - v0.4.2

1 个答案:

答案 0 :(得分:1)

好的,我找到了解决方法。我永远无法让这个自动完成的Angular版本工作。另外,当我console.log(o); autocomplete.angular.js中的构造函数function Dataset(o) {}时,它会将其记录两次。不确定这是不是问题...但是普通的autocomplete.js并没有记录两次。

自从交换到autocomplete.j后,自动完成功能一直很好。以下是我使用的代码:

autocomplete('#quicksearch', { hint: false, debug: true, openOnFocus: true }, [
{
  source: autocomplete.sources.hits(settings.replica, { hitsPerPage: 5 }),
  displayKey: 'address',
  name: 'address',
  templates: {
    suggestion: function(suggestion) {

       var address = '<span class="address">' + suggestion._highlightResult.address.value;

       if(suggestion.aptmnt) {
        address += ', Unit ' +suggestion.aptmnt + '</span> | ';
       } else {
        address +='</span> | '; 
       }

       address += '<span>' + suggestion.totbdrms + ' beds </span> | ' +
        '<span>' + suggestion.totbaths + ' bath </span> | ' +
        ' <span> $' + suggestion.price + '</span>';
        return address;
    }
  }
},{
  source: autocomplete.sources.hits(settings.zipcode, { hitsPerPage: 5 }),
  displayKey: 'zipcode',
  name: 'zipcode',
  templates: {
    suggestion: function(suggestion) {
       var zipcode = '<span class="zipcode">' + suggestion.zipcode + '</span>';
        return zipcode;
    }
  }
}

]);