我有一个选择框,我正在使用一个角度对象作为值,即
{identityName:'Passport',identityId:1}
<select name="identityProof" ng-model="identityProof" ng-change="changeProofOfIdentity()" ng-options="identity as identity.identityName for identity in identityProofList track by identity.identityId" id="identityProofList" class="form-control" data-placeholder="" size="1"></select>
我已经完成了这个设置,所以我可以在ng-onchange函数中获得身份id和身份标签。如您所见,我已经使用了track by identity.identityId
,我想通过only by identityId来选择该选项
$scope.identityProof = {identityId:userDetails.identityId,identityName:""};
现在,具有给定值的选项被选中,但当我尝试获取$scope.identityProof (即选择框值)时,我只得到名称,而不是identityName,因为我可以看到选中的选项同时具有名称和值。
我怎么才能两者兼得呢?
我是否需要使用jquery或javascript来管理它,通过获取所选值的标签并将其作为identityName传递?
或者有一个选项可以让我重新加载选定的对象以同时拥有这两个值?
发布于 2018-12-14 13:40:45
您可以使用简单的select而不是ng-option
。请看下面的例子,让我知道。
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<select name="identityProof" ng-model="identityProof" size="1">
<option ng-repeat="identity in identityProofList" value="{{identity}}">
{{identity.identityName}}
</option>
</select>
<span ng-if="identityProof">Selected value = {{identityProof}}</span>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.identityProofList = [{'identityName':'Passport','identityId':1}];
});
</script>
</body>
</html>
https://stackoverflow.com/questions/53773906
复制相似问题