1

My question is very simple, but I can't get it. I have a select to order elements in AngularJS with custom options. I just need to get one option selected when the page loads.

Here's my code:

<select ng-model="sortBy">
    <option value='priceOrder'>Price order</option>
    <option value='price'>Price - +</option>
    <option value='-price'>Price + -</option>
    <option value='weight'>Weight + -</option>
    <option value='-weight'>Weight - +</option>
</select>

The values comes from a Json in my controller.js, and I ask for order in:

<li ng-repeat="item in filteredGenere | filter:query | orderBy:sortBy" >.
   {{item.brand}} {{item.name}}
</li>

1 Answer 1

2

If you want the select box has one option selected when page loaded, you need to initialize $scope.sortBy in your controller like

$scope.sortBy = 'priceOrder';

Check this demo

Sign up to request clarification or add additional context in comments.

2 Comments

@ManelMusti Next time, do more research there are a lot of answer about your case...
@ManelMusti You can also initialize it using ng-init="sortBy = 'priceOrder'"

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.