ngOptions"跟踪由"前pressionngOptions、QUOT、pression

由网友(22.野兽派掌门)分享简介:我想'被跟踪前pression使用由ID来跟踪选项,在对象的数组。不过,我似乎无法使其工作就像我想它的工作原理。I am trying to use the 'track by' expression to track selections by id, in an array of objects. However...

我想'被跟踪前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

阅读全文

相关推荐

最新文章