typeahead angular ui - 无法读取undefined属性'length'

Lic*_*hte 7 angularjs angular-ui angular-ui-bootstrap

我在我的应用程序中使用Angular-ui Bootstrap.我使用typeahead指令.

HTML:

<input type="text" class="pass_code_input" ng-model="SuppPrefix" Typeahead="ddl_item.Text for ddl_item in getData($viewValue)"/>
Run Code Online (Sandbox Code Playgroud)

调节器

function AutoCompleteCtrl($scope,$http, AutoCompleteSrv) {
    $scope.getData = function (prefix) {
        AutoCompleteSrv.GetAutoComplete("Supplier", prefix).then(function (result) {
            var results_arr = [];
            angular.forEach(result.data, function (item) {
                results_arr.push({ "Val": item.Val, "Text": item.Text });
            });
            return results_arr
        });  
    };
};
Run Code Online (Sandbox Code Playgroud)

服务:

    function AutoCompleteSrv($http) {
    var _$http = $http;
    self = this;
    self.GetAutoComplete = function (DataType, Prefix) {
        var promise = _$http({
            method: "GET",
            url: 'api/AutoComplete',
            params: { 'DataType': DataType, 'Prefix': Prefix }
        }).success(function (data, status, headers, config) {

        }).error(function (data, status, headers, config) {

        });
        return promise;
    };
};
Run Code Online (Sandbox Code Playgroud)

我从服务器接收数据,但我无法在屏幕上显示它.当我在chrome dev工具中运行调试器时,我收到下一个错误:

TypeError: Cannot read property 'length' of undefined
at http://localhost:52145/js/libs/ui-bootstrap-tpls-0.11.0.min.js:13:12650
at m.promise.then.u (http://localhost:52145/js/angular/angular.min.js:97:280)
at m.promise.then.u (http://localhost:52145/js/angular/angular.min.js:97:280)
at http://localhost:52145/js/angular/angular.min.js:98:417
at h.$eval (http://localhost:52145/js/angular/angular.min.js:108:482)
at h.$digest (http://localhost:52145/js/angular/angular.min.js:106:62)
at h.$apply (http://localhost:52145/js/angular/angular.min.js:109:287)
at HTMLInputElement.l (http://localhost:52145/js/angular/angular.min.js:133:191)
at http://localhost:52145/js/angular/angular.min.js:31:32
at q (http://localhost:52145/js/angular/angular.min.js:7:386)
Run Code Online (Sandbox Code Playgroud)

我搜索了在多个地方的解决方案,像那个,也跟着一步一步的引导用户界面主页上的说明进行操作.我做错了什么?

Sco*_*ott 6

我刚碰到这个并解决了它.这里的关键是,在角度站点上给出的示例中,它们"返回"$ http结果.这让人感到困惑,但最终这才是重要的回报.因此,在您的情况下,您要将变量设置为该返回值,以便永远不会返回返回值.当您的代码以这种方式格式化时,您需要做两件事来改变它:第一个是"return"语句在错误的位置.第二个是返回值的声明也在错误的位置.以下是示例中的非工作代码:

.....
then(function(res){
      var addresses = [];
      angular.forEach(res.data.results, function(item){
        addresses.push(item.formatted_address);
      });
      return addresses;
    });
Run Code Online (Sandbox Code Playgroud)

这是我改变它以使其工作的方式.

var addresses = []
....
then(function(res){
      angular.forEach(res.data.results, function(item){
        addresses.push(item.formatted_address);
      });
    });
    return addresses;
Run Code Online (Sandbox Code Playgroud)

如果您不使用"then"而是使用"success"和"error"函数,则会变得更加清晰.然后很明显返回语句应该位于何处以及返回值声明应该位于何处.以这种方式编写代码并使其工作是我如何找出问题所在.请注意,您应该清除"then"函数中的addressses值,否则它将继续追加越来越多的值.