Angularjs选择ng-repeat中选择框的初始值

时间:2014-04-17 11:52:43

标签: angularjs angularjs-scope angularjs-ng-repeat angular-ngmodel

我想在最初的选择框中将所选颜色填充为所选选项

jsfiddle

的链接

这是我的部分:

<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>

控制器:

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 个答案:

答案 0 :(得分:3)

试试这个:

<select ng-model="product.selected" ng-options="color.name for color in product.color track by color.id"></select>