0

I have the following code:

<select ng-model="sortYear">
    <option value="all">All Years</option>
    <option ng-repeat="article in news | orderObjectBy:'year':true | unique: 'year'" value="{{article.year}}">{{article.year}}</option>
</select>

Which works perfectly. I've even got sortYear bound at a different point on the page, and it's connected fine, etc.

My problem is, when I get to the page, I want the default, prechosen selection to be the top actual year, not "All Years". I've tried a couple things:

1) I set $scope.sortYear in my controller. I know it's setting it since the bound sortYear later in the page is showing up just fine on the initial load.

2) I tried using ng-init to set it to a year explicitly, as well as tried the ng-init="sortYear = getCurYear()" where I defined a function getCurYear in the controller (and verified that I was actually hitting that function and setting/returning what I thought I should be) but nada.

What am I missing?

4
  • 1
    You really should use ngOptions instead of ngRepeat for selects. Commented Sep 3, 2015 at 13:51
  • so, in the initial select tag I would have ng-options="article in news | orderObjectBy:'year':true | unique: 'year'" ? Would that set my default? Commented Sep 3, 2015 at 13:53
  • Assuming your sortYear was set correctly it should. Commented Sep 3, 2015 at 13:55
  • It didn't work. :( I know sortYear is set correctly since it's being used elsewhere, but using the ng-options didn't set the default. It still uses the top value of "All Years" Commented Sep 3, 2015 at 14:00

1 Answer 1

1

use ng-value in option tag with ng-repeat like

<option ng-value="article.year" ng-repeat="article in news | orderObjectBy:'year':true | unique: 'year'">{{article.year}}</option>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.