title: AngularJS: ng-model switching int to string
body: <p>I'm currently working on an app in Angular. So far, everything has been going -quite- well. I'm really, <em>really</em> new to angular and am amazed that it took so long for the first real roadblock.</p>
  <p>Situation:</p>
  <p>I have an array of objects each with an order.</p>
  <pre><code>category.items = [{id: 1, order: 1, type: {}, ...}, {id: 54, order: 2, type: {}, ...}, {id: 3, order: 3, type: {}, ...}]
  </code></pre>
  <p>The user needs to be able to rearrange these items. The new order must be set to the object property 'order'.</p>
  <p>In html these objects are rendered like so:</p>
  <pre><code>&lt;div class=&quot;category&quot;&gt;
      &lt;div class=&quot;item&quot; ng-repeat=&quot;(itemIndex, item) in category.items track by $index&quot;&gt;
          &lt;div class=&quot;header&quot;&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  <p>In the header-div I have an inputfield, type select.</p>
  <pre><code>&lt;select ng-model=&quot;item.order&quot;  ng-change=&quot;changeItemOrder((itemIndex + 1), item.order, itemIndex)&quot;&gt;
    &lt;option ng-repeat=&quot;item in category.items&quot; ng-value=&quot;($index + 1)&quot;&gt;{{$index + 1}}&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  <p>The code for changeItemOrder:</p>
  <pre><code>$scope.changeItemOrder = function(old_order, new_order, item_index) {
      new_order = parseInt(new_order);
      if (old_order != new_order) {
          var upper = Math.max(old_order, new_order);
          var lower = Math.min(old_order, new_order);
  
          angular.forEach($scope.category.items, function(item, key) {
              if (item_index != key) {
                  if (new_order &lt; old_order) {
                      if (item_index &gt;= new_order &amp;&amp; (key + 1) &gt;= lower &amp;&amp; (key + 1) &lt;= upper) {
                          item.order = (parseInt(item.order) + 1);
                      }
                  } else if (new_order &gt; old_order) {
                      if (item_index &lt;= old_order &amp;&amp; (key + 1) &lt;= upper &amp;&amp; (key + 1) &gt;= lower) {
                          item.order = (parseInt(item.order) - 1);
                      }
                  }
              } else {
                  item.order = parseInt(new_order);
              }
          });
  
          $scope.reorderItems();
      }
  };
  </code></pre>
  <p><em>(ReorderItems just call angular sorting with a default sorting mechanism comparing the orders and returning -1, 1 or 0.)</em></p>
  <p>Here is where I discovered/spotted/pinpointed one of the breaking bugs in one of the possible solutions for this problem. Here I noticed that my INT is converted to string somehow, as on render an option is added to the dropdown with value 'string:2'.</p>
  <p>I've tried ng-options, in all possible ways, but even those led to problems.
  The way I did ng-options was by doing item.order as item.order in ... and so on, that just made the order switch around until somehow all items had the same order. Trying different grouping methods or trackbys just gave different bugs, like suddenly introducing NaN en NULL in the dropdown, or completely removing the order property as a whole from the item-object.</p>
  <p>So far the least bug-ridden solution has been using the ng-repeat on my options. That only causes a mismatch on the type of item.order.</p>
  <p>Now, after searching far and wide, spending hours on stackoverflow (especially before writing up this question with that nifty little question-searching thingy) I come to you.</p>
  <ol>
  <li><p>How can I halt/circumvent the behavior where my item.order is switched from INT to STRING?</p>
  </li>
  <li><p>If that isn't possible, how can I force my $index to be a string, so the model(string) matches the value(string)</p>
  </li>
  <li><p>If that isn't possible, how can I write my ng-options so that I get the behavior I want? ( I've seriously tried a lot, from track by to different as and for statements, all resulted in different bugs)</p>
  <p>On initial load, all selects show a correct value selected, so all item.order are initially INT (I get them from our API), it's only after interacting that all but the object that triggered the reorder get messed up.</p>
  </li>
  </ol>