ng 选项 select angular js

ng options select angular js

当我尝试加载具有我留给您的格式的排列选项时,第一个选项是空白的,以下是我排列的值。当我尝试 select 另一个我无法获得价值的选项时,这对我来说是个问题。

<select ng-model='form.type' 
    ng-options="item.name for item in organizations" 
    ng-change="update({{form.type.id}})">
</select>

格式:

{"status":0,"Area":[{"id":"1","name":"Marketing","private":null},{"id":"2","name":"Ventas","private":null},{"id":"5","name":"Soporte","private":null}],"Count":3}

控制器:

$scope.organizations = data.Area;

您的情况是您的 ng-model 变量 form.type 不包含 select 下拉列表中选项的值。这就是您看到空行的原因。例如,如果你想让它成为第一个,你可以这样做(我改为 selected 变量,但它根本不重要):

$scope.organizations = [{"id":"1","name":"Marketing","private":null},{"id":"2","name":"Ventas","private":null},{"id":"5","name":"Soporte","private":null}];

$scope.selected = $scope.organizations[0].id;

现在,selected 变量具有您选择的 id。但这还不是全部。您还需要稍微修改 HTML,使 ng-options 具有 id 作为您的值。它可以是您在对象中想要的任何内容,不一定是 id,但这就是为什么我认为您需要,您可以更改它:

<select ng-model='selected' ng-options="item.id as item.name for item in organizations" ng-change="update({{selected}})">

Fiddle