0

i am not getting value in select box every time it's showing me first element place blank. i dont know why can any one tell me how to overcome this problem.

i am attaching problem what exactly problem that i am facing please see in image as well as sending my html.

https://i.stack.imgur.com/jh3tH.png

https://i.stack.imgur.com/XWt21.png

Html

<div class="form-group">
                        <label  class="col-lg-3 form-label">Brand:</label>
                        <div class="col-lg-5">

                            <select ng-show="categoryShow" class="form-control" ng-model="item.brand_id"  >
                                <option value="{{brand._id}}">{{brand.name}}</option>
                                <option ng-repeat="brand in brands" value="{{brand._id}}">{{brand.name}}</option>
                            </select>
                        </div>
                        <div class="col-lg-4">
                            <a ng-click='openBrandModel()'>
                                <i class="icon-plus-sign"></i>
                                Add Brand
                            </a>
                        </div>
                    </div>

data is perfectly binding everything good just not showing by default selected that value which i used out side ng-repeat directive.

In image u can see aslam name so i want by defuat its showing and remaing as a list.

Js for setting brand and brands

itemService.getCategoryAndBrand.TypeGetCategoryAndBrand({}, $cookies.token, function(response){
    if(response.brands){

        var catchBrands = [];

        for(var i = 0; i < response.brands.length; i++){
            if(response.brands[i]._id == catchSelectedBrand){
                $scope.brand = response.brands[i];

            }else{
                catchBrands.push(response.brands[i]);
            }
        }

        $rootScope.brands = catchBrands;
    }
    if(response.categories){

        var catchCategories = [];

        for(var i = 0; i < response.categories.length; i++){
            if(response.categories[i]._id == catchSelectedCategory){
                $scope.category = response.categories[i];

            }else{
                catchCategories.push(response.categories[i]);
            }
        }

        $rootScope.categories = catchCategories;

    }

});
Andyrooger
  • 6,748
  • 1
  • 43
  • 44
Wajihurrehman
  • 567
  • 3
  • 15
  • 29

1 Answers1

0

I don't think you need the first option in your HTML.

<option value="{{brand._id}}">{{brand.name}}</option>

All of your options look like they come from the line below with the ng-repeat, and at this point there doesn't appear to be a brand scope variable so I would expect to see an option with blank value and name.

If you remove this first line then I expect to see the blank option disappear, though I have not tried and do not know any logic around angular or your browser putting a blank option here by default...


Edit

Actually seeing this answer, it looks fairly possible you just need to make sure item.brand_id is set to the _id of an existing options.

Community
  • 1
  • 1
Andyrooger
  • 6,748
  • 1
  • 43
  • 44
  • ok my first option that write is basically that value which i want to show on top that's why i wrote seperated and second thing when i saw html it's showing me by default this where this line come from when i am deleting explicilty in delete then working fine any idea about this thanx – Wajihurrehman Nov 17 '13 at 01:07
  • i also removed first line as u told me but still i dont where it from adding this line that's its not empty sapce. – Wajihurrehman Nov 17 '13 at 01:10
  • Can you post the JS where you set `brand` and `brands`? – Andyrooger Nov 17 '13 at 01:11
  • Is `item.brand_id` initally set to `catchSelectedBrand` to set the default properly? If this is done then I suspect you can delete the line in my answer (and replace your `if(response.brands)` block with `catchBrands = response.brands` if you like). If that doesn't work then I'm not sure I have any other ideas... – Andyrooger Nov 17 '13 at 01:39