您好我是AngularJS的新手,我正在尝试从我的数据库中获取数据到我的控制器以创建自动完成搜索栏。 这是我的代码,我正在连接到我的数据库并收集我需要的数据:
router.get('/request', function (req, res) {
connection.then(function(){ //connection is my sql.connect to the confing
new sql.Request().query("SELECT FullName from Employee").then(function(recordset){
res.render('request',{json: JSON.stringify(recordset)});
}).catch(function(err){
console.log("Connection Error " + err);
});
});
});
这是我的控制器代码:
app.controller('autocompleteCtrl', ['$scope', '$http', function($scope, $http){
var self = this;
self.data = null;
self.selectedItem = null;
self.searchText = null;
self.querySearch = function(){
$http.get('/users/request').then(function(result){
self.data = result.data.Search;
return result.data.Search;
});
}
}]);
以下是视图中的代码:
<md-autocomplete md-floating-label="Employee name"
flex=""
md-item-text="item.FullName"
md-items="item in ctrl.data"
md-search-text-change="ctrl.querySearch(ctrl.searchText)"
md-search-text="ctrl.searchText"
md-selected-item="ctrl.selectedItem"
md-no-cache="ctrl.noCache"
md-input-maxlength="30"
md-input-minlength="3"
md-input-name="autocompleteField"
required="">
<md-item-template>
<span md-highlight-text="ctrl.searchText">{{item.FullName}}</span>
</md-item-template>
<div ng-messages="searchForm.autocompleteField.$error" ng-if="searchForm.autocompleteField.$touched">
<div ng-message="required">You <b>must</b> have a favorite movie.</div>
<div ng-message="minlength">Your entry is not long enough.</div>
<div ng-message="maxlength">Your entry is too long.</div>
</div>
</md-autocomplete>
当我尝试在搜索栏内输入时,它会给我错误:
无法读取未定义的属性'然后'
我不知道怎么做...请帮帮我! 谢谢!