我一直在学习Angular,并试图在模拟中实现它。当我尝试实现Angular typeahead时,typeahead的结果显示为undefined可以有人建议我哪里出错了吗? html的代码片段,
<input type="text" id="search" ng-model = "selectedEnterprise" class="form-control form-design input-md enable collapse" placeholder="search for an enterprise" uib-typeahead = "enterprise.name as vm.enterprises.name for enterprise in vm.enterprises | filter:$viewValue | limitTo : 3 ">
控制器代码段
$http.get(path + '/enterprise/all/' + num)
.then(function success(response) {
vm.enterprises = response.data;
temp = vm.enterprises;
console.log(vm.enterprises);
}, function error(response) {
console.log(response);
});
&#13;
我在后端使用MongoDB和NodeJS框架。
答案 0 :(得分:2)
更改您的预先输入代码
来自
<input type="text" id="search" ng-model = "selectedEnterprise" class="form-control form-design input-md enable collapse" placeholder="search for an enterprise" uib-typeahead = "enterprise.name as vm.enterprises.name for enterprise in vm.enterprises | filter:$viewValue | limitTo : 3 ">
要
<input type="text" id="search" ng-model = "selectedEnterprise" class="form-control form-design input-md enable collapse" placeholder="search for an enterprise" uib-typeahead = "enterprise.name as enterprise.name for enterprise in vm.enterprises | filter:$viewValue | limitTo : 3 ">
在typeahead中,我们正在为vm.enterprises(数据源)创建一个引用对象企业,并且从该引用对象中我们提取名称变量并要求它将其分配给ng-model
uib-typeahead = "enterprise.name as enterprise.name for enterprise in vm.enterprises
答案 1 :(得分:0)
而不是
uib-typeahead = "enterprise.name as vm.enterprises.name for enterprise in vm.enterprises | filter:$viewValue | limitTo : 3
表达式应该像
uib-typeahead = "name as enterprise.name for enterprise in vm.enterprises | filter:$viewValue | limitTo : 3
您应该引用特定实例enterprise.name
而不是所有实例vm.enterprises.name