mysql - 带有mysql的NodeJS rest api无法迭代输出格式

标签 mysql angularjs node.js rest angularjs-http

伙计们,我遵循了关于如何使用 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/

相关文章:

php - 如何在php mysql中使用limit来获取新记录?

mysql - 外键已接受但不起作用

javascript - $event.stopPropagation() 运行但父事件仍然执行

javascript - 明示 promise 行不通

javascript - 将 req.query ['name' ] 的值保存到 Node.jS 中的变量中?

mysql - 显示与逗号相关的两个表之间的树结果

javascript - 使用 ionicPopup.show 在弹出窗口中将按钮移动到彼此下方

javascript - 最佳实践)在 Angularjs 1.x 中处理复杂的 UI 交互

javascript - 使用await时 undefined object

mysql - MySQL 可以在开发计算机上本地设置吗?