1

I have a select dropdown in a form, it's not passing value to {{service}} object:

<select name="categoryId" ng-model="service.categoryId" ng-options="category.id as category.name for category in categories track by category.categoryId" required>
  <option value="0">-select a category-</option>
</select>

And I can't find a reason why, anyone?

1
  • what do you mean? you can't get the value of 'service.categoryId'? Commented Feb 18, 2016 at 9:40

1 Answer 1

1

Check this example, it may help you:

function theController($scope) {

  $scope.categories = [
    {name:'First', id:'A', categoryId: 1},
    {name:'Second', id:'B', categoryId: 2},
    {name:'Third', id:'C', categoryId: 3}
  ];
  
  $scope.service = $scope.categories[1];
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<body ng-app>
    
  <div ng-controller="theController">
    <select name="categoryId" 
            ng-model="service" 
            ng-options="category as category.name for category in categories track by category.categoryId" required>
      <option value="">-select a category-</option>
    </select>
    <pre>categories = {{categories|json}}</pre>
    <pre>service = {{service|json}}</pre>
  </div>
</body>

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

1 Comment

I ended up using this: <option ng-repeat="category in categories" value="{{category.categoryId}}" ng-selected="{{option=='id'}}">{{category.name}}</option> works too, thanks for your answer

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.