Javascript AngularJS引导程序TypeAhead中出错:TypeError:无法读取属性';长度';未定义的
我在尝试从AngularUI引导实现AngularJS Typeahead时遇到以下错误: (我只是调用一个servlet,它以JSON格式返回结果) HTMLJavascript AngularJS引导程序TypeAhead中出错:TypeError:无法读取属性';长度';未定义的,javascript,angularjs,twitter-bootstrap,servlets,typeahead,Javascript,Angularjs,Twitter Bootstrap,Servlets,Typeahead,我在尝试从AngularUI引导实现AngularJS Typeahead时遇到以下错误: (我只是调用一个servlet,它以JSON格式返回结果) HTML <h4>Users from local service</h4> <pre>Model: {{userList | json}}</pre> <input type="text" ng-model="userList" placeholder="Users load
<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>
Servlet
$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;
});
};
来自servlet的响应
我已提及以下问题:
然而,我仍然无法找到解决方案。servlet本身的响应是否存在任何问题?还是其他原因?在仔细查看代码后,我注意到了问题所在,您必须在此处返回$http PROMITE,注意在$http之前返回
return
你能给我们一个从你的后端输出的例子吗?看起来打字机需要一个数组here@maurycy在问题中添加了响应的快照。很酷,看起来不错,在返回之前,您能记录
用户吗?它应该是一个字符串数组,它向我显示如下数组:users=[“Test user2”,“Test user3”,“Test user4”,“Test user5”,“Test user6”]我的坏,我一加上“return”就开始工作了:)你救了我一天!容易忽视,祝你度过愉快的一天:)
$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());
}
$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;
});
};