0

I am going through the Angularjs tuts and docs given in the official angularjs website.

Here we add a selectbox for ordering, like this

  <select ng-model="orderProp">
   <option value="name">Alphabetical</option>
   <option value="age">Newest</option>
  </select> 

and in the controller, we assign $scope.orderProp = "age" This works fine and 'Newest' will be the default select option.

Then I tried putting the order items to a model and populate the selectbox using ng-repeat like this.

  <select ng-model="orderProp">
    <option ng-repeat="orderby in orderProperties" value="{{orderby.criteria}}">{{orderby.property}}</option>
  </select>

where:

orderProp model is :

function PhoneListCtrl($scope){

    $(document).ready($scope.orderProp = "age");

        $scope.orderProperties = [
            {"property":"Alphabetical","criteria":"name"},
            {"property":"Newest","criteria":"age"}
        ];
}

In dom I can see the value="name" and value="age". But this time the default value is not set as "Newest". I don't understand why the hardcoded value="age" is working, while value="{{orderby.criteria}}" is not. Can anyone please guide me through.

2
  • I've found, that the value is getting set during $scope.orderProp = "age" and sorting is getting triggered. But the selectbox <select ng-model="orderProp"> is still empty. I am assuming it might be due something like assigning value before page load or something like that and might not be related to angularjs. Commented Mar 22, 2013 at 10:22
  • You should give a look here. It's explained how to use select. And you don't need $(document).ready(...). Commented Mar 22, 2013 at 10:29

2 Answers 2

2

You are using select in the wrong way:

<select ng-model="orderProp" 
    ng-options="orderby.criteria as orderby.property for orderby in orderProperties">
</select>

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

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

Comments

0

You should use ng-option to populate the select.

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.