【发布时间】:2015-06-30 11:28:42
【问题描述】:
我正在使用ng-options 生成一个选择标签,其选项是位置。标签是位置名称,值是位置 ID(在数据库中)。
我已将值(位置 ID)绑定到 ng-model 属性,但我还想将标签(位置名称)绑定到不同的 ng-model 属性。 (我需要分隔 id 字段,因为这将被发布到需要此特定属性的服务器。)在 Angular 中执行此操作的最佳方法是什么?
我的代码:
<div ng-app="app"><div ng-controller="edit">
<select ng-model="purchase.pickUpLocationId" ng-options="loc.id as loc.name for loc in purchase.availableLocations"></select>
<!-- This is the model not yet bound: -->
<p>You have selected {{ purchase.pickUpLocationName }}</p>
</div></div>
var app = angular.module('app', []);
app.controller('edit', ['$scope', function($scope) {
$scope.purchase = {
pickUpLocationId: 30,
availableLocations: [
{id: 20, name: "Charleston, SC"},
{id: 30, name: "Atlanta, GA"},
{id: 40, name: "Richmond, VA"},
]
};
}]);
【问题讨论】:
标签: javascript html angularjs angular-ngmodel ng-options