草庐IT

javascript - AngularJS-Bootstrap TypeAhead : TypeError: Cannot read property 'length' of undefined 错误

coder 2024-07-15 原文

我在尝试从 AngularUI-Bootstrap 实现 AngularJS Typeahead 时遇到以下错误: (我只是调用一个以 JSON 格式返回结果的 servlet)

TypeError: Cannot read property 'length' of undefined
    at http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.js:3553:24
    at wrappedCallback (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.js:10930:81)
    at wrappedCallback (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.js:10930:81)
    at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.js:11016:26
    at Scope.$eval (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.js:11936:28)
    at Scope.$digest (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.js:11762:31)
    at Scope.$apply (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.js:12042:24)

HTML

<h4>Users from local service</h4>
    <pre>Model: {{userList | json}}</pre>
    <input type="text" ng-model="userList" placeholder="Users loaded from local database" 
    typeahead="username for username in fetchUsers($viewValue)" 
    typeahead-loading="loadingUsers" class="form-control">
    <i ng-show="loadingUsers" class="glyphicon glyphicon-refresh"></i>

JS

$scope.fetchUsers = function(val) {
        console.log("Entered fetchUsers function");
        $http.get("http://localhost:8080/TestWeb/users", {
            params : {
                username : val
            }
        }).then(function(res) {
            var users = [];
            angular.forEach(res.data, function(item) {
                users.push(item.UserName);
            });
            return users;
        });
    };

小服务程序

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        UserUtil userUtil = new UserUtil();
        List userList = userUtil.fetchUsers();
        Iterator userIterator = userList.iterator();

        JSONArray users = new JSONArray();


        while(userIterator.hasNext()) {
            UserDetails userDetails = (UserDetails)userIterator.next();

            JSONObject jo =  new JSONObject();
            jo.put("UserID", userDetails.getUserId());
            jo.put("UserName", userDetails.getUserName());
            jo.put("UserDescription", userDetails.getDescription());

            users.add(jo);
        }

        response.setContentType("application/json");
        response.getWriter().write(users.toString());


    }

servlet 的响应

我提到了以下问题:

但是,我仍然无法找出解决方案。 servlet 本身的响应是否有任何问题?还是其他原因?

最佳答案

第二次查看代码后我发现了问题,你必须在这里返回 $http promise,注意 return before $http

$scope.fetchUsers = function(val) {
        console.log("Entered fetchUsers function");
        return $http.get("http://localhost:8080/TestWeb/users", {
            params : {
                username : val
            }
        }).then(function(res) {
            var users = [];
            angular.forEach(res.data, function(item) {
                users.push(item.UserName);
            });
            return users;
        });
    };

关于javascript - AngularJS-Bootstrap TypeAhead : TypeError: Cannot read property 'length' of undefined 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25114911/

有关javascript - AngularJS-Bootstrap TypeAhead : TypeError: Cannot read property 'length' of undefined 错误的更多相关文章

随机推荐