angularstrap typeahead使用对象

时间:2015-02-12 20:29:17

标签: typeahead angular-strap

当我尝试在对象上使用bs-typeahead时出现错误。当我开始在文本框中输入

时,会显示错误消息

下面是html:

<input type="text" class="form-control input-lg"  id="city" name="city" ng-model="data.cityState" placeholder="City" ng-options="subscribedTown.id as subscribedTown.label  for (index, subscribedTown) in data.subscribedTowns" bs-typeahead>

以下是js:

var oostApp = angular.module('oostApp', ['mgcrea.ngStrap']);

oostApp.controller('SearchController', ['$scope', function ($scope) {
  $scope.data = {};

  $scope.data.cityState = '';

  $scope.data.subscribedTowns = {

    '54bd7820104d630153f62a06': {
      id: "54bd7820104d630153f62a06", label: "Bloomfield, CT", name: "Bloomfield", state: "CT"
    },

    '54bd7829104d630153f66082': {
      id: "54bd7829104d630153f66082", label: "Manchester, CT", name: "Manchester", state: "CT"
    }

  };

}]);

以下是plunker: http://embed.plnkr.co/XFMMUfTLmmvNXn2quO0Y

我在Google Chrome中遇到的错误是

TypeError: undefined is not a function
    at i (typeahead.js:259)
    at typeahead.js:259
    at l.promise.then.F (angular.js:11573)
    at l.promise.then.F (angular.js:11573)
    at angular.js:11659
    at k.$get.k.$eval (angular.js:12702)
    at k.$get.k.$digest (angular.js:12514)
    at k.$get.k.$apply (angular.js:12806)
    at angular.js:1447
    at Object.d [as invoke] (angular.js:3966)

1 个答案:

答案 0 :(得分:-1)

我不知道为什么你在对象中使用对象并将对象id作为键赋予它,而对象具有相同的id值。我认为使用数组更好,这也可能会产生你得到的错误。 id值包含在每个对象中,因此不需要将其复制为整个对象的键。迭代数组更容易,更快速,AngularStrap上的在线示例也使用数组。 资料来源:https://mgcrea.github.io/angular-strap/#/typeaheads#typeaheads

通过这种方式,您可以直接进入对象并获取使用AngularStrap中的typeahead所需的所有值。

这是一个可能的解决方案: http://plnkr.co/edit/5AewKg?p=preview

我现在得到的undefined function来自Plunker。我不是每次都得到这个,当我在本地测试代码时它永远不会出现。请让我知道这对你有没有用。输入 B 将触发两种可能性。

另一个小记:

angular.module('appName', []); // This is the angular module setter
angular.module('appName'); // This is the angular getter

来源:github.com/johnpapa/angularjs-styleguide#modules