2014-06-18 4 views
0

<select> 태그를 미리 선택된 값으로 설정하려고합니다. 불행히도 ui-select2 module이 지원하지 않기 때문에 ng-options 지시어를 사용할 수 없습니다. 여기 옵션 및 개체 비교가 포함 된 각도 선택 태그

은 예제 (또는, as a Plunker if you would prefer) : 나는 $scope.selectedColor 첫 번째 색상 객체와 같은 객체임을 깨닫고 올바르게 미리 선택하는 각도 예상이 경우

// Controller 
$scope.colors = [{ 
    name: 'blue', 
}, { 
    name: 'red', 
}, { 
    name: 'green' 
}]; 

$scope.selectedColor = $scope.colors[0]; 


<!-- HTML --> 
<select ng-model="selectedColor"> 
    <option ng-repeat="color in colors" value="{{color}}">{{color.name}}</option> 
</select> 

하지만에 표시되지 않습니다 작업.

ng-options을 사용하거나 이름을 비교하여이 문제를 해결할 수 있지만 이러한 솔루션 중 어느 것도 적합하지 않습니다.

각도 선택에서 개체를 올바르게 비교하려면 어떻게해야합니까?

답변

0

ui-select2과 함께 ng-options을 사용하는 것으로 보이는 온라인 예가 발견되어 필요로하는 것과 잘 작동하는 것 같습니다. Plunker here을 참조하십시오.

HTML

<select ui-select2 
    ng-model="selectedColor" 
    ng-options="color.name for color in colors"> 
</select> 

컨트롤러

$scope.colors = [ 
{ 
    name: 'blue', 
}, 
{ 
    name: 'red', 
}, 
{ 
    name: 'green' 
}]; 

$scope.selectedColor = $scope.colors[1];