Setting default element, it only works if I do something like `{{ organizations[referral.organization] }}` elsewhere on the page. – Joshua Dutton Apr 18 '13 at 21:31

  • Exactly. I always forget I can create my own jsfiddle or something to accompany a question. – Joshua Dutton Apr 18 '13 at 21:36
  • 1
    I'm guessing you'll have to do this: http://jsfiddle.net/qBJK9/2/, since you need a reference to an object that exists in memory. Otherwise you can structure your `select` so that it just uses simple values and can easily be updated from a value from the server. Unless there's some Angular hack that does it for you? I'm not seeing anything though. – Langdon Apr 18 '13 at 21:43
  • 0

    You can assign referral.organization to one of objects obtained from ajax:

    $scope.referral.organization = $scope.organizations[0]
    

    I created simple demo in plunker. Button click handler changes list of objects and selects default one.

    $scope.changeModel = function() {
      $scope.listOfObjects = [{id: 4, name: "miss1"},
                              {id: 5, name: "miss2"},
                              {id: 6, name: "miss3"}];
      $scope.selectedItem = $scope.listOfObjects[1];
    };
    
    Vasiliy Kevroletin
    • 1,188
    • 13
    • 17
    0

    The other answers were correct in that it usually "just works." The issue was ultimately a mismatch between the organization key (an integer) stored inside $scope.organizations and the key as stored in the $http response, which is stored in JSON and therefore as a string. Angular was not matching the string to the integer. As I mentioned in my edit to the original question, the solution at the time was to cast the $http response data to a string. I am not sure if current versions of Angular.js still behave this way.

    Joshua Dutton
    • 1,190
    • 8
    • 11