0

Angular Bootstrap Typeahead に promise を渡そうとしていますが、常に次のエラーが発生します: TypeError: Cannot read property 'length' of undefined

工場:

    angular.module('app').factory('geoCoding', ['$http', 'geoUtils', function ($http, geoUtils) {

  var request= function(location){
    return $http.get('http://open.mapquestapi.com/geocoding/v1/address',{params:{location:location}});
  };

  var ret = {};

  ret.locate = function (location) {
    return request(location).then(function (data) {
      if (data  && data.data && data.data.results && data.data.results[0]) {
        var locations = [];
        data.data.results[0].locations.forEach(function (location) {
          locations.push({name: geoUtils.location2String(location), location: location});
        });
        return locations;
      }
    });
  };
  return ret;
}]);

コントローラ:

$scope.getLocations = function(){
    console.log('scope',$scope.inputLocation);
    return geoCoding.locate($scope.inputLocation);
  }

テンプレート:

        <input type="text" class="form-control oo-focus" placeholder="Search" typeahead="location as location.name for location in getLocations()" typeahead-wait-ms="500" ng-model="inputLocation" />

古いバージョンの Angular Bootstrap-UI 0.5.0 では、すべて正常に動作していました。問題は Bootstrap-UI 0.6.0 (bootstrap3_bis2) にあります。 https://github.com/angular-ui/bootstrap/tree/bootstrap3_bis2

4

2 に答える 2

2

AngularJS 1.2.0-rc.2 を使用している場合は、promise に問題があります (promise の自己参照の公開が変更されました – またはそのようなもの:))。理由ははっきりとは言えませんが、ここで詳細を確認できます。

Angularjs 1.2.0rc2 で promise を使用するとタイプヘッドが壊れる

私にとっての解決策は、promise.$$v 変数を設定することです。したがって、あなたの例では、コードは次のようになります。

$scope.getLocations = function(){
   console.log('scope',$scope.inputLocation);
   var promise = geoCoding.locate($scope.inputLocation);
   promise.$$v = promise;
   return promise;
}

これはハッキーだという著者 (wilsonjackson) の意見に同意します。とにかくそれがあなたを助けることを願っています。

于 2013-09-25T09:05:14.913 に答える
2

Angular 1.2.0-rc.3 が出ました。そのバージョンで問題なく動作します: http://code.angularjs.org/1.2.0-rc.3/

于 2013-10-15T08:54:05.873 に答える