title: AngularJS: need to attach a selected collection to a model
body: <p>I'm building a CRUD and I want to some model to be linked to others. So on some update/create template I need to add some other model and save their id when submitting the form.
  So I did a basic form that is served by my backend. Here is the part where I try to display the linked model:</p>
  
  <pre><code>&lt;div ng-repeat="item in otherItems"&gt;
      &lt;select ng-model="item" name="item" &gt;
          &lt;option value="1" ng-selected="item"&gt;
          &lt;option value="2" ng-selected="item"&gt;
          &lt;option value="3" ng-selected="item"&gt;
      &lt;/select&gt;
      &lt;a class="remove" ng-click="removeRelated(item)"&gt;remove&lt;/a&gt;
  &lt;/div&gt;
  &lt;a ng-click="addRelated()"&gt;&lt;i class="icon-plus"&gt;&lt;/i&gt;&lt;/a&gt;
  </code></pre>
  
  <p>Note: otherItems could be empty at the beginning (when doing a create or when an item is not linked to any other related model's item).
  So when one press add/remove it will trigger a controller's function:</p>
  
  <pre><code>$scope.addRelated = function (){
      if (typeof $scope[otherItems]=="undefined")
          $scope[otherItems] = new Array();
  
      $scope[otherItems].push($scope[otherItems].length);
  };
  
  $scope.removeRelated = function (item){
      console.debug(item);
      var idx = $scope[otherItems].indexOf(item);
      if (idx !== -1) {
          $scope[otherItems].splice(idx, 1);
      }
  };
  </code></pre>
  
  <p>My problem is when I save I get the position of item in items (so it's always 0, 1, 2...) I won't have an array of selected ID. 
  I guess there is something wrong with my addRelated maybe. What am I doing wrong?</p>
  
  <p>Here is a screenshot to understand the idea since I may not be very clear:
  <img src="https://i.stack.imgur.com/DZ0eL.png" alt="enter image description here"></p>