【问题标题】:AngularJS 1.2.0 Bootstrap UI 0.6.0 (bootstrap3_bis2) TypeaheadAngularJS 1.2.0 Bootstrap UI 0.6.0 (bootstrap3_bis2) Typeahead
【发布时间】:2013-10-15 16:54:05
【问题描述】:

我正在尝试向 Angular Bootstrap Typeahead 传递一个承诺,但我总是遇到以下错误: TypeError:无法读取未定义的属性“长度”

工厂:

    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

【问题讨论】:

    标签: angularjs angular-ui angular-ui-bootstrap angularjs-bootstrap


    【解决方案1】:

    如果您使用的是 AngularJS 1.2.0-rc.2,则 Promise 会出现问题(更改了 Promise 自我引用的公开方式——或者类似的想法:))。我真的不能告诉你为什么,但你可以在这里找到更多信息:

    Typehead broken when using promises in Angularjs 1.2.0rc2

    对我来说,wiking 解决方案是设置 promise.$$v 变量。所以在你的例子中,代码应该是:

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

    我同意作者 (wilsonjackson) 的观点,认为这太骇人听闻了!无论如何希望它会帮助你。

    【讨论】:

      【解决方案2】:

      Angular 1.2.0-rc.3 已经发布,与该版本兼容:http://code.angularjs.org/1.2.0-rc.3/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多