2

Possible Duplicate:
How to make a preselection for a select list generated by AngularJS?

In the angular tutorial they set a sortOrder scope variable that defaults the sort by select to 'Newest'.

In my fiddle I am doing similar but my select value is not selected. Instead i get the blank entry as if i did not set any value in the scope. Yet I can see my sort order and i am emitting the value of the select and it is preset.

    //set a default this way also, but not updating the dropdown
$scope.ddlFilter = {
    name: "All Open Events",//doesn't matter what this is, only cares about value
    value: "ALL"//"OPEN"
};

I know i can use ng-init to set defaults but i want to be able to do it this way as well. Any suggestions?

Community
  • 1
  • 1
VBAHole
  • 1,508
  • 2
  • 24
  • 38
  • See the link above -- your answer is `$scope.ddlFilter = $scope.eventFilters[2];` -- the ng-model must reference an element from the same array used in ng-options. – Mark Rajcok Jan 31 '13 at 03:51

1 Answers1

6

Try using an object instead of an array of objects. It will allow you to reference what you want by a string vs. an array position such as scope.eventFilters[0].

$scope.eventFilters = {
    "OPEN": "All Open Events",
    "UNMAPPED": "All Un-Mapped Events",
    "MAPPED": "All Mapped Events",
    "CLOSED": "All Closed Events (past 8 hours)",
    "ALL": "All Events",
    "OTHER": "Other OpCenter Open Events"
};

And then define the default value as a string (key for the object above):

$scope.ddlFilter = "MAPPED";

And then on the page change your select to

<select ng-model="ddlFilter" ng-options="k as v for (k, v) in eventFilters"></select>

and your filter no longer needs a .value, so it will become:

<tr ng:repeat="event in events  | myEventFilter:ddlFilter | orderBy:sort.column:sort.descending">

See your modified code working here: http://jsfiddle.net/WXLte/2/

Jay
  • 18,959
  • 11
  • 53
  • 72
  • very nice. thank you much. – VBAHole Jan 31 '13 at 04:55
  • I have run into a slight issue with this one unfortunately. The dropdown that i want to fill not only contains those values i have listed here (which you helped me get into an object instead of an array) but there is also a dynamic set of options i need to glom onto this set to be loaded in the dropdown. Those extra values come from an $http call and they come in as name/value pairs. How can i blend those new values with this existing set? – VBAHole Jan 31 '13 at 19:12
  • 1
    Make a new question that is more specific to this new issue - This one has been closed. There are a lot of people who can help you if you make it a new question (you can link back to this question if you'd like) – Jay Feb 01 '13 at 14:12
  • 1
    And, I think what you're looking for is http://docs.angularjs.org/api/angular.extend (It will extend the current object with new properties). It works a lot like [jQuerys](http://api.jquery.com/jQuery.extend/) version. – Jay Feb 01 '13 at 14:15