我已经在nodejs中编写了一个API,它将根据给定的客户端名称获取所有相关记录。并且在角度我使用下拉列表选择具有唯一名称的客户端名称。到目前为止它工作正常,但我希望显示所有相关数据(如果一个客户端名称包含2-3个不同的记录)。但是当我选择客户端名称时,它只获取一行数据。
这是我使用邮递员检查过的工作API:
# Free all resources
dbClearResult(rs)
# Close connection
dbDisconnect(con)
在角度我用下面的代码获取所有数据并按引号表中的客户名称过滤:
router.get('/getbyname/:clientname',function(req,res){
var clientname= req.params.clientname;
Quotation.find({clientname: clientname} ,function(err,user){
if(err){
res.json({ success: false, message: 'Can\'t display details'});
}else{
res.json(user);
}
});
});
在视图中我有这样的选择:
var onPersonGetCompleted01 = function(response){
$scope.quotations = response.data;
//console.log($scope.quotations);
}
var refreshQuotation = function(){
$http.get('/api/getallquotations')
.then(onPersonGetCompleted01);
console.log('Response received...');
}
var onGetByName = function(response){
$scope.clientname = response.data;
console.log(response.data);
};
$scope.searchPerson = function(clientname){
$http.get('/api/getbyname/' + clientname)
.then(onGetByName);
console.log(clientname);
};
根据客户端名称选择数据没问题。但我只是想知道如何使用相同的客户端名称显示相关数据。由于有超过2-3个文件。
如果我这样使用,它只会获取一行的数据:
<select ng-model="clientSelected" ng-options="quote.clientname for quote in quotations | unique:'clientname'" ng-click="searchPerson(quote.clientname)">
<option value="0">Default</option>
答案 0 :(得分:0)
如果您想要显示具有相同客户名称的多个文档,那么我们应该在ng-options部分中使用| unique:'clientname'
重播track by $index
。因此,最终的ng-options代码将为ng-options="quote.clientname for quote in quotations track by $index"
答案 1 :(得分:0)
我认为我已接近获得所需的输出,现在我可以在选择标记稍作更改后在控制台中获取所有相关值(行)。< / p>
<select class="chosen-select form-control col-sm-3" ng-
model="clientSelected" ng-options="quote.clientname for quote in
quotations | unique: 'clientname'" ng-
click="searchPerson(clientSelected.clientname)" >
<option value="0">Default</option>
</select>
最初我传递 quote.clientname searchPerson(quote.clientname)
作为参数,现在我将其更改为searchPerson(clientSelected.clientname)
。它现在工作正常。