2

In Angularjs, I have a DropDown:

<select ng-model="category" ng-change="categoryChanged(category)" class="form-control" 
data-ng-options="category as category.name for category in categories">
        <option value="">Select Category</option>
</select>

And I have a controller:

app.controller('searchBoxController', ['$scope', '$location', '$routeParams', 'categoryService', function ($scope, $location, $routeParams, categoryService) {
        categoryService.getParentCategory().$promise.then(
        function (model) {
            $scope.categories = model;
            $scope.category.id = $routeParams.categoryId;// Which is coming as "1"
        },
        function (error) {

        });

        $scope.categoryChanged = function (category) {
             alert(category.id);
        };
}]);

$routeParams.categoryId is coming as "1" but still it is not setting the selected option in the dropdown.

3
  • 1
    Possible duplicate of How do I set default value of select box in angularjs Commented Jan 18, 2017 at 8:16
  • You have to declare $scope.category = {}; at the beginning of your controller. Commented Jan 18, 2017 at 8:17
  • @xkcd149 Sorry to say but my question is not duplicate. I had only id from $routeParams whereas the question which is marked as duplicate is focusing on whole object. Commented Jan 18, 2017 at 9:39

2 Answers 2

2

Your categories variable is an array of objects, while you set the ng-model to an object with only an id. Because it is a whole new object, angular doesn't see it as a match of the category in your array, and won't select the correct one.

The solution is to set the $scope.category to the matching object of the array instead of creating a new one:

var id = $routeParams.categoryId;
// Find the category object with the given id and set it as the selected category
for (var i = 0; i < $scope.categories.length; i++){
    if ($scope.categories[i].id == id) {
        $scope.category = $scope.categories[i];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You have to change ng-model directive when promise operation is end.

<select  ng-model="category" ng-change="categoryChanged(category)" class="form-control" data-ng-options="category as category.name for category in categories">
    <option value="">Select Category</option>
</select>

Suppose you have var id = $routeParams.categoryId and its value is 2 for example. You have to find category from categories where category.id is 2. For this, the simpliest method is to use a filter method.

$scope.category = $scope.categories.filter(function(item){
        return item.id==$scope.id;
})[0];

Please take a look to working fiddle

1 Comment

I was not the downvoter, but I can see your given solution doesn't solve OP's question. You just set the category to the first item of the array instead of the one with the given id of the $routeParams

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.