ngOptions"跟踪由"前pression [英] ngOptions "track by" expression

查看:160
本文介绍了ngOptions"跟踪由"前pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想'被跟踪前pression使用由ID来跟踪选项,在对象的数组。不过,我似乎无法使其工作就像我想它的工作原理。

I am trying to use the 'track by' expression to track selections by id, in an array of objects. However, I can't seem to make it work like I think it works.

//ids from server
$scope.serverDTO = ['1','2','3'];

//composed objects from the ID set
$scope.composedData = [{id:1,name:"test"},{id:2,name:"test"},{id:3,name:"test"}];

<!-- select box -->
<select ng-model="serverDTO" ng-options="item as item.name for item in composedData track by item.id"></select>

因此​​,基于该文档我虽然上载的选项指令会看到serverDTO对轨道由1,2,3,和IDS有那些pre-选择。用户修改了选择之后,我需要做这样的事情到阵列返回到服务器 -

So based on the documentation I though that the options directive on load would see that the serverDTO has the 'track by' ids of 1, 2, and 3, and have those pre-selected. After the user modifies the selection I would need to do something like this to return the array to the server-

//recreate proper DTO [1,2,3];
$scope.serverDTO = $scope.serverDTO.map(function(val){
  return val.id;
});

我是遥远这个应该是怎样工作的?

Am I way off on how this is supposed to work?

推荐答案

轨道只是帮助角与内部阵列据我所知排序。该选项的值由第一个参数定义(在你的情况项目)。如果你希望它是由ID,那么你应该使用 item.id作为item.name在项目项

track by just helps Angular internally with array sorting as far as I know. The value of the options is defined by the first argument (in your case item). If you want it to be by id then you should use item.id as item.name for item in items

这篇关于ngOptions&QUOT;跟踪由&QUOT;前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆