伙计们,我遵循了关于如何使用 node+express+mysql 实现 rest api 的教程,这是我的代码 REST.js 文件中的一条路线:
router.get("/companies",function(req,res){
var query = "SELECT * FROM ??";
var table = ["company"];
query = mysql.format(query,table);
connection.query(query,function(err,rows){
if(err) {
res.json({"Error" : true});
} else {
res.json({"Companies" : rows});
}
});
});
这就是我使用 Angular 调用它们的方式:
sampleApp.controller('InstallController', function($scope, $http) {
$http.get('http://localhost:3000/api/companies').
success(function (data) {
$scope.clients = data;
});
}
和html
<option ng-repeat="client in clients">{{client}}</option>
这是我得到的回应:
{
"Companies": [
{
"id": 1,
"name": "GOLF"
},
{
"id": 2,
"name": "RENAULT"
},
{
"id": 3,
"name": "AUDI"
},
{
"id": 4,
"name": "MAZDA"
}
]
}
我的目标是迭代结果以使用名称填充选择标签。请耐心等待,我要开始了。谢谢:)
最佳答案
你应该在下面使用,但是当你选择任何值时,你只能在 selectedClient
中有 id
值。
<select ng-model="selectedClient">
<option value="client.id" ng-repeat="client in clients.Companies">
{{client.name}}
</option>
</select>
更好的选择是在 select
下拉菜单上使用 ng-options
指令,这样您就可以在此处选择对象而不是原始值。当您选择 GOLF
选项时,您将拥有 {"id": 1, "name": "GOLF"}
对象,无论是否作为 ng- 的重复选项repeat
对通过它选择原始类型值有限制。
<select ng-model="selectedClient" ng-options="client.name for client in clients.Companies"></select>
关于mysql - 带有mysql的NodeJS rest api无法迭代输出格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36209944/