Javascript AngularJS typeahead在Ajax GET查询中给出[filter:notarray]错误
我前面有一个AngularJS 1.4.7类型:Javascript AngularJS typeahead在Ajax GET查询中给出[filter:notarray]错误,javascript,angularjs,ajax,angular-ui-typeahead,Javascript,Angularjs,Ajax,Angular Ui Typeahead,我前面有一个AngularJS 1.4.7类型: <input type="text" class="form-control simpleinput" placeholder="keyword" name="searchbarFormInput" id="searchbarFormInput" ng-model="selected" ng-click="selected=null"
<input type="text"
class="form-control simpleinput"
placeholder="keyword"
name="searchbarFormInput"
id="searchbarFormInput"
ng-model="selected"
ng-click="selected=null"
ng-enter="doBlur($event)"
uib-typeahead="record.display_name for record in getRecords($viewValue) | filter:{ display_name : $viewValue }"
typeahead-template-url="searchbar-result-template.html"
typeahead-on-select="selectRecord($item)"
typeahead-focus-on-select="false"
typeahead-editable="false"
autocomplete="off" />
这里,$scope.testRecords
是一个测试对象数组,例如:
$scope.testRecords = [ { "display_name" : "foo" } , { "display_name" : "bar" } ... ];
这是正确的。typeahead将呈现一个下拉菜单。菜单根据我在输入
字段中键入的内容进行过滤
现在,我想从GET查询中获取此记录数组的源:
$scope.getRecords = function(needle) {
return $http.get('/search/by', {
params: {
'requestType' : 'searchByDisplayName',
'pageSize' : 1000
}
}).then(function(res) {
console.log(res.data.results);
return res.data.results;
});
}
这是对路由的GET请求,该路由在直接调用时有效(例如通过curl
或wget
确认)
另外,当我查看浏览器控制台时,console.log(res.data.results)
显示了正确的对象数组-与数组$scope.testObjects
中的对象完全相同
但是,我没有得到下拉菜单,我还得到以下[filter:notarray]
错误:
Error: [filter:notarray] http://errors.angularjs.org/1.4.7/filter/notarray?p0=%7B%22%24%24state%22%3A%7B%22status%22%3A0%7D%7D
https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:6:421
https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:149:197
fn
R@https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.14.3/ui-bootstrap-tpls.min.js:11:6694
https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.14.3/ui-bootstrap-tpls.min.js:11:8633
$$parseAndValidate@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:263:425
$commitViewValue@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:263:284
https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:265:279
$eval@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:133:221
$apply@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:133:456
$$debounceViewValueCommit@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:265:249
$setViewValue@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:264:507
l@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:163:165
c@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:35:221
当我进入输入
字段时,此错误重复出现的字母数与我输入的字母数相同
我对请求、typeahead或typeahead筛选器有何错误?我如何解决此问题?在不起作用的代码中,您返回的是承诺(是.then()的结果),而不是数组:
res.data.results
编辑:
期望表达式的结果生成数组而不是承诺。换句话说,您应该使用change事件触发函数,该函数通过承诺更新数组,但是表达式的“for record In nameofarray”部分不应该是函数。它应该是$scope上的一个变量。当它改变时,它应该触发摘要和更新。交叉检查对象返回是否为
array
类型,我认为您还应该检查我是否记录以下内容:console.log(res.data.results instanceof array)
我在每次按键时都会得到true
。您是否浏览了我提供的github问题链接..我返回的对象-res.data.results
-是一个数组的实例
,因此我不确定您链接的github问题如何解释这个问题,除非我遗漏了一条与此无关的具体评论。你是什么意思?这就是uib typeahead
directive expect如何处理承诺,当出现数据时,它将其分配给列表选项。我没有投票否决你,但我不确定我是否理解你的答案。谢谢你的回答。如果你有时间的话,请你详细说明一下好吗?
res.data.results