NodeJS rest api with mysql 无法迭代输出格式

NodeJS rest api with mysql cannot iterate on the output format

伙计们,我遵循了关于如何使用 node+express+mysql 实现休息 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"
    }
  ]
}

我的目标是迭代结果以使用名称填充 select 标记。请耐心等待,我要开始了。谢谢:)

您应该在下面使用,但是当您 select 任何值时,您只能在 selectedClient 中包含 id 值。

<select ng-model="selectedClient">
    <option value="client.id" ng-repeat="client in clients.Companies">
      {{client.name}}
    </option>
</select>

更好的选择是在 select 下拉菜单上使用 ng-options 指令,因此您可以在此处 select 对象而不是原始值。就像你在 select GOLF 选项时会有 {"id": 1, "name": "GOLF" } 对象一样,作为 ng-repeat 的重复选项是否限制了 select 原始类型值。

<select ng-model="selectedClient" ng-options="client.name for client in clients.Companies"></select>

看起来您已将所有内容正确连接在一起,您只需更改 $scope.clients 的分配即可。

$scope.clients = data.Companies;

然后在你的 HTML

<option value="{{client.id}}" ng-repeat="client in clients"> 
   {{client.name}}
</option>