0

我想填充所選擇的顏色作爲選擇框選擇的選項,在最初鏈接Angularjs選擇內選擇框初始值NG-重複

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 } 
      } 
     ];  
} 

回答

3

試試這個:

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

它的工作能否請您解釋我是如何工作???? –

+1

使用「track by」表達式,您可以指示angular使用「id」屬性來考慮兩個對象而不是對等關係。 – ppa