[英]Angular ng-option access multiple properties
Say I have a backend call which returns a list of objects (and properties), and I use ng-option to stick one of the properties (the name) into the dropdown list, which is using ng-model to attach itself to my model object. 假设我有一个后端调用,该调用返回一个对象(和属性)列表,并且我使用ng-option将其中一个属性(名称)粘贴到下拉列表中,该列表使用ng-model将自身附加到我的模型上宾语。
My problem comes in when I need to access other properties of the selected object. 当我需要访问所选对象的其他属性时,就会出现我的问题。 ng-option lets me bind objects to that dropdown, which is great. ng-option使我可以将对象绑定到该下拉列表,这很棒。 However, if I pull the name out to bind that to my model: 但是,如果我拉出名称以将其绑定到模型:
<select ng-model="myModel.name" ng-options="fieldlist.fields.name as fieldlist.fields.name for fieldlist in metrics">
I lose reference to the rest of the object's properties. 我没有引用该对象的其余属性。 I need to use another property of the selected object, say fieldlist.fields.location
, to perform some other action in an ng-change function. 我需要使用所选对象的另一个属性,例如fieldlist.fields.location
,才能在ng-change函数中执行其他操作。 So 所以
Is this possible? 这可能吗? Is my Angular naiveté showing too much? 我的Angular天真显示太多吗?
I believe you can do: 我相信你可以做到:
ng-model="myModel" ng-options="fieldlist.fields as fieldlist.fields.name for fieldlist in metrics"
Assuming you want myModel to contain all the fields in the selected fieldlist 假设您希望myModel包含所选字段列表中的所有字段
Per your comment - and this may not be best practice, just my initial thought on how I would do it, you could do something like: 根据您的评论-这可能不是最佳做法,只是我对如何做的最初想法,您可以执行以下操作:
ng-model="selectedItem" ng-change="setSelectedItem()" ng-options="fieldlist.fields.name as fieldlist.fields.name for fieldlist in metrics"
and then in your controller 然后在您的控制器中
$scope.setSelectedItem() = function() { $scope.myModel.name = selectedItem.name; };
or remove the ng-change and do 或删除ng-change并执行
$scope.watch(selectedItem, function() { $scope.myModel.name = $scope.selectedItem.name }
And then you can get whatever property you need from $scope.selectedItem
然后,您可以从$scope.selectedItem
获得所需的任何属性。
A little late, but good information i found if anyone could use it. 有点晚了,但是我发现是否有人可以使用它是很好的信息。 Its still a little odd, but it makes it a little cleaner. 它仍然有点古怪,但是使它更干净。 Angular released a ngModelOptions directive which, among other useful things, allows you to define the ngModel as a getter/setter function. Angular发布了ngModelOptions指令,该指令除其他有用的功能外,还允许您将ngModel定义为getter / setter函数。 Using this, you can remove that pesky ng-change or watch (and possibly a $Digest?). 使用此功能,您可以删除该烦人的ng-change或手表(可能还有$ Digest?)。 Simply place your update function as the ng-model and set your items that way. 只需将您的更新功能放置为ng-model并以这种方式设置您的项目。
<select ng-model="updatefielset" ng-model-options="{getterSetter:true}" ng-options="fieldlist.fields as fieldlist.fields.name for fieldlist in metrics">
$scope.updatefielset(item) = function()
{
if(angular.isDefined(item)
{
$scope.myModel.A = item.A;
$scope.myModel.B = item.B;
$scope.Val = item;
}
return $scope.Val;
};
https://docs.angularjs.org/api/ng/directive/ngModelOptions https://docs.angularjs.org/api/ng/directive/ngModelOptions
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.