Algolia角度自动完成无法正常工作

时间:2016-03-14 02:54:39

标签: angularjs algolia

我正试图获得Angolia autcomplete指令,用于角度工作但遇到麻烦。我试过跟随docs中的示例。我遇到了一些问题。

具体在getDatasets我目前收到错误b.$watchCollection is not a function

我做了plunkr。有没有人有使用algolia角度指令的工作示例?

  $scope.q = '';
  var client = algolia.Client('latency', '6be0576ff61c053d5f9a3225e2a90f76');
  var index = client.initIndex('contacts');
  $scope.getDatasets = function() {
    return {
      source: function(q, cb) {
        index.search(q, { hitsPerPage: 5 }, function(error, content) {
          if (error) {
            cb([]);
            return;
          }
          cb(content.hits);
        });
      },
      templates: {
        suggestion: function(suggestion) {
          return suggestion._highlightResult.name.value;
        }
      }
    };
  };
  $scope.$watch('q', function(v) {
    console.log(v);
  });
  $scope.$on('autocomplete:selected', function(event, suggestion, dataset) {
    console.log(suggestion, dataset);
  });

1 个答案:

答案 0 :(得分:1)

我试过你的plunkr,似乎你的角度版本有点老了,我已经用示例和it works中的那个更新了它。

// the main (app) module
angular
	.module("myApp", ['algoliasearch', 'algolia.autocomplete'])
	.controller("myCtrl", function($scope, algolia) {
    $scope.q = '';
      var client = algolia.Client('latency', '6be0576ff61c053d5f9a3225e2a90f76');
      var index = client.initIndex('contacts');
      $scope.getDatasets = function() {
      console.log('getting datasets')
        return {
          source: function(q, cb) {
            index.search(q, { hitsPerPage: 5 }, function(error, content) {
              if (error) {
                cb([]);
                return;
              }
              cb(content.hits);
            });
          },
          templates: {
            suggestion: function(suggestion) {
              return suggestion._highlightResult.name.value;
            }
          }
        };
      };
      $scope.$watch('q', function(v) {
        console.log(v);
      });
      $scope.$on('autocomplete:selected', function(event, suggestion, dataset) {
        console.log(suggestion, dataset);
      });
});
.algolia-autocomplete {
        width: 100%;
      }
      .algolia-autocomplete .aa-input, .algolia-autocomplete .aa-hint {
        width: 100%;
      }
      .algolia-autocomplete .aa-hint {
        color: #999;
      }
      .algolia-autocomplete .aa-dropdown-menu {
        width: 100%;
        background-color: #fff;
        border: 1px solid #999;
        border-top: none;
      }
      .algolia-autocomplete .aa-dropdown-menu .aa-suggestion {
        cursor: pointer;
        padding: 5px 4px;
      }
      .algolia-autocomplete .aa-dropdown-menu .aa-suggestion.aa-cursor {
        background-color: #B2D7FF;
      }
      .algolia-autocomplete .aa-dropdown-menu .aa-suggestion em {
        font-weight: bold;
        font-style: normal;
      }
<script src="//cdn.jsdelivr.net/jquery/2.1.4/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.2/angular.min.js"></script>
<script src="//cdn.jsdelivr.net/algoliasearch/3/algoliasearch.angular.min.js"></script>
<script src="//cdn.jsdelivr.net/autocomplete.js/0/autocomplete.angular.min.js"></script>



<body ng-app="myApp" ng-controller="myCtrl">
    <div class="container">
      <form action="#">
        <div class="autocomplete-wrapper">
          <input id="contacts" name="contacts" type="text" ng-model="q" autocomplete aa-datasets="getDatasets()" />
        </div>
      </form>
    </div>
</body>