0

I want to populate the selected color as selected option in selectbox in initially

link of jsfiddle

here is my partial :

<div ng-app ng-controller="QuestionController">
    <ul ng-repeat="product in products">
        <li>
            <div>{{ product.selected | json }}</div>
            <select ng-model="product.selected" ng-options="color.name for color in product.color"></select>
         </li>
    </ul>
</div>

controller :

function QuestionController($scope) {
    $scope.products = [
             {
                "name": "product1", 
                "value": "product1",
                "color": [{ name: 'red',id: 10},{ name: 'Green',id: 11},{name:'Blue',id: 12 }],
                "selected": {name: 'Green', id: 11 }
             },
             {
                "name": "product2",
                "value": "product2",
                "color": [{ name: 'red',id: 10},{ name: 'Green',id: 11},{name:'Blue',id: 12 }],
                "selected": {name: 'red', id: 10 }
             },
             {
                "name": "product3",
                "value": "product3",
                "color": [{ name: 'red',id: 10},{ name: 'Green',id: 11},{name:'Blue',id: 12 }],
                "selected": {name: 'Blue',id: 12 }
            }
        ];    
}

1 Answer 1

3

Try this:

<select ng-model="product.selected" ng-options="color.name for color in product.color track by color.id"></select>
Sign up to request clarification or add additional context in comments.

1 Comment

With the "track by" expression, you can instruct angular to use the "id" property in order to consider two objects equivalent instead of using object equality.

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.