Angularjs autoselect dropdowns from model

I'm trying display some data loaded from a datastore and it's not reflecting changes on the UI. I created an example to show a general idea of what I'm trying to achieve.

http://plnkr.co/edit/MBHo88

Here is the link to angularjs example where they show when on click then dropdowns are clear out. If you replace the expression with one of the colors of the list dropdowns are well selected. Does this type of selection only work on user events?

http://docs.angularjs.org/api/ng.directive:select

Help is appreciated!!!

Actually the problem is that ngSelect compares objects using simple comparition operator ('=='), so two objects with same fields and values are considered as different objects.

So you better use strings and numbers as values ('select' parameter in expression of ngSelect directive).

Here is kind of solution for your plunker.

Aslo there are some discussion about this topic on GitHub: https://github.com/angular/angular.js/issues/1302 https://github.com/angular/angular.js/issues/1032

Also as I headred there is some work in progress about adding custom comparor/hashing for ngSelect to be able to use ngSelect more easier on objects.

One mistake in the initialization of your controller. You have to refer to the objects in your palette, since these are watched on the view:

$scope.selectedColors.push({col: $scope.palette[2]});
$scope.selectedColors.push({col: $scope.palette[1]});

Same with your result:

$scope.result = { color: $scope.obj.codes[2] };

Then you need to watch the result. In the below example, select 1 receives the value from the initiating select, the second receives the value below in the palette. I don't know if that's what you wanted, but you can easily change it:

$scope.$watch('result', function(value) {
   if(value) {
      var index = value.color.code -1;
      $scope.selectedColors[0] = {col: $scope.palette[index] };
      $scope.selectedColors[1] = {col: $scope.palette[Math.max(index-1, 0)] };
   }
}, true);

See plunkr.

Ok, I think I figured this out but thanks to @ValentynShybanov and @asgoth.

According to angularjs example ngModel is initialized with one of the objects from the array utilized in the dropdown population. So having an array as:

$scope.locations = [{ state: 'FL', city: 'Tampa'}, {state: 'FL', city: 'Sarasota'} ....];

And the dropdown is defined as:

<select ng-options="l.state group by l.city for l in locations" ng-model="city" required></select>

Then $scope.city is initialized as:

$scope.city = $scope.locations[0];

So far so good, right?!!!.. But I have multiple locations therefore multiple dropdowns. Also users can add/remove more. Like creating a table dynamically. And also, I needed to load data from the datastore.

Although I was building and assigning a similar value (e.g: Values from data store --> State = FL, City = Tampa; Therefore --> { state : 'FL', city : 'Tampa' }), angularjs wasn't able to match the value. I tried diff ways, like just assigning { city : 'Tampa' } or 'Tampa' or and or and or...

So what I did.. and I know is sort of nasty but works so far.. is to write a lookup function to return the value from $scope.locations. Thus I have:

$scope.lookupLocation = function(state, city){
   for(var k = 0; k < $scope.locations.length; k++){
      if($scope.locations[k].state == state && $scope.locations[k].city == city)
         return $scope.locations[k];
   }
   return $scope.locations[0]; //-- default value if none matched
}

so, when I load the data from the datastore (data in json format) I call the lookupLocation function like:

$scope.city = $scope.lookupLocation(results[indexFromSomeLoop].location.state, results[indexFromSomeLoop].location.city);

And that preselects my values when loading data. This is what worked for me.

Thanks