title: Dynamically allocating class to ng-repeat items with ng-options select
body: <p>I have a select which uses ng-options. The selected value of this populates a ng-repeat list using filter. Now I need to add a class to some of the ng-repeat list items. I have the 'id's of the list elements, to which the class is to be added, in an array.</p>
  
  <p>Html Code:</p>
  
  <pre><code>&lt;select class="form-control" ng-options="car.BaseStation as car.BaseStation for car in UnUsedCars | unique: 'BaseStation' | orderBy:'BaseStation'" ng-model="selected.BaseStation" ng-change="locationChanged()"&gt;
      &lt;option value=""&gt;All Locations&lt;/option&gt;
  &lt;/select&gt;
  
  &lt;h3&gt;Select Car&lt;/h3&gt;
  &lt;ul class="cablist"&gt;
      &lt;li ng-repeat="unUsedCar in UnUsedCars | filter: selected.BaseStation" id="{{unUsedCar.Id}}"&gt;
          &lt;div class="cardetails"&gt;
              &lt;h4&gt;{{unUsedCar.CarType}}&lt;/h4&gt;
          &lt;/div&gt;
      &lt;/li&gt;                        
  &lt;/ul&gt;
  </code></pre>
  
  <p>js</p>
  
  <pre><code>&lt;script data-require="angular-ui@*" data-semver="0.4.0" src="http://rawgithub.com/angular-ui/angular-ui/master/build/angular-ui.js"&gt;&lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
      var carApp = angular.module('carApp', ['ui.directives','ui.filters']);
  
      carApp.controller('CarCtrl', ['$scope', function(scope) {
  
          scope.locationChanged = function() {
              arrayLength = scope.carArray.length; //scope.carArray is the array of li element Id on which class "selected" is to be added.
              for(i = 0; i &lt; arrayLength; i++) {
                  var carId = scope.carArray[i]; 
                  document.getElementById(carId).className += " selected";
              }
          }      
  
      }]);
  </code></pre>
  
  <p>Example of data array</p>
  
  <pre><code>[{
      "Id" : "1",
      "CarType" : "Audi",
      "BaseStation" : "Canada"
  },
  {
      "Id" : "2",
      "CarType" : "BMW",
      "BaseStation" : "U.S.A"
  },
  {
      "Id" : "3",
      "CarType" : "Benz",
      "BaseStation" : "Canada"
  }]
  </code></pre>
  
  <p>scope.carArray data example</p>
  
  <pre><code>["2","3"]
  </code></pre>
  
  <p>When I choose another location from select with ng-options my js is suppose to add class "selected" to the li items, which has their ids in scope.carArray. It some times works, but most times it crashes. What are the mistakes I made here?</p>