预输入角度UI - 无法读取未定义的属性“长度”长度、属性、角度、未定义

2023-09-13 04:35:09 作者:姐的心禁止访问

我采用了棱角分明的UI引导我的应用程序。我用的是预输入指令。

I am using Angular-ui Bootstrap for my application. I use the typeahead directive.

HTML

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

控制器

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
        });  
    };
};

服务:

    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;
    };
};

我从服务器收到的数据,但我不能在屏幕上显示。当我在Chrome浏览器开发工具来运行调试器,我得到下一个错误:

I do recieve the data from server, but I can't display it on the screen. When I run debugger in chrome dev tools, I get the next error:

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)

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

I've searched for solution in multiple places, like that, and also followed step by step the instructions in the bootstrap ui home page. What did I do wrong?

推荐答案

我只是遇到了这个,只是解决了它。这里的关键是,在对角网站给出的例子,他们回归的$ HTTP结果。这使得它混乱看看,但最终那就是重要的回报。所以你的情况要设置到返回值的变量,这样的回报永远不会回来了。当你的code这样的格式,你需要做两件事情来改变它:第一个是返回语句是放错了地方。第二个是,返回值的声明也是在错误的地方。下面是从例如非工作code:

I just ran into this and just solved it. The key here is that in the example given on the angular site they "return" the $http results. That makes it confusing to look at but ultimately that is the return that matters. So in your case you are setting a variable to that return value so the return never gets returned. When your code is formatted in this way you will need to do 2 things to change it: The first is that the "return" statement is in the wrong place. The second is that the declaration for the returned value is also in the wrong place. Here is the non-working code from the example:

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

下面是我如何改变它来得到它的工作。

Here is how I changed it to get it working.

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

如果你不使用的然后,而是用成功和错误的功能,这一点就更清楚了。然后,它变得很明显,其中return语句应放哪里返回值的声明应该的。编写code的方式得到它的工作就是我想通了这个问题。请注意,您应在再功能清除出地址添加值,否则只会继续追加更多的值。

This becomes more clear if you do not use the "then" but instead use the "success" and "error" functions. It then becomes obvious where the return statement should lie and where the returned value declaration should be. Writing the code that way and getting it to work is how I figured out the problem. NOTE that you should clear out the addressses value in the "then" function or else it will just keep appending more and more values.