我一直在尝试在我的模型中实现预先输入功能,其中表格内容会在typeahead的值发生更改时刷新,但会引发错误说明,
Angularjs错误:[filter:notarray]预期数组但收到:{}
vm.getEnterprises = function(term){
vm.selected = undefined;
return $http.get(path +'/enterprise/'+ term + '/search')
.then(function(response){
vm.customEnterprises = response.data;
vm.showcontent = true;
vm.defaultcontent = false;
return response.data;
console.log(response);
});
}
我使用MongoDB和Node中的Mongoose ORM作为后端,API代码是,
var mongoose = require('mongoose');
var enterprise = mongoose.model('enterprise');
var search = function(req, res){
var name = req.params.name;
var regexValue = '\.*' + name ;
enterprise.find({'name': new RegExp(regexValue, 'i')},function(err, data){
if (err){
console.log('err',err);
} else {
return res.json(data);
console.log(data);
}
});
}
module.exports = {
searchEnterprise : search
};
我的Angular UI代码是,
input type="text" id="search" ng-model = "vm.selected" class="form-control input-md enable collapse" placeholder="search for an enterprise" uib-typeahead = "name as enterprise.name for enterprise in vm.getEnterprises($viewValue)| filter:$viewValue | limitTo : 3 ">
答案 0 :(得分:0)
据推测,您知道filter
仅接受Array
作为输入,您的vm.getEnterprises
基本上会返回一个承诺对象而非实际数据。
您需要做的只是将退货数据分配到范围级变量(即与模板相同的范围),例如$scope.retData
然后将模板更改为
name as enterprise.name for enterprise in retData | filter:$viewValue | limitTo : 3