Frees
Frees

Reputation: 149

Angular selecting default value in ng-select doesn't work with resources

select directive and two resources from which resource Product.get gets product object which contains product.brand item and is used as ngmodel for ng-select directive, while second resource Brands.query returns array of brand objects which are used to create ngOptions param of ng-select directive.

$scope.product = {
  "id":"15",
  "photo":"15-15-1-cedule-soutez-2015-05-30-11-53-38-5569a4c2bef33.png",
  "name":"Hello produkt 3",
  "brand":"7",
  "price":"29",
  "currency":"Kč",
  "points":"10"
}

$scope.brands = [
  {
     "id":"1",
     "name":"Marlboro",
     "profile":"1",
     "rival":"0"
  },
  {
     "id":"7",
     "name":"Chesterfield",
     "profile":"1",
     "rival":"0"}
]

<select data-placeholder="Select brand" 
        class="w-lg" 
        ng-options="brand.id as brand.name for brand in brands track by brand.id" 
        ng-model="product.brand">
            <option value=""></option>
        </select>

The problem is, that I'm not able to set selected value for this ng-select to item 7, Chesterfield. Every time is selected first empty value. I found many solutions for these problem, but in my conditions none of them worked. Thank you so much for your help.

Upvotes: 0

Views: 715

Answers (1)

AJ Richardson
AJ Richardson

Reputation: 6820

From the ngOptions documentation:

Do not use select as and track by in the same expression. They are not designed to work together.

Instead, try:

ng-options="brand.id as brand.name for brand in brands" 

Upvotes: 1

Related Questions