2014-07-15 2 views
1

AngularJs 사용 & 부트 스트랩.AngularJs의 색인

각 li 항목의 색인을 얻으려고하면 각 항목마다 색을 다르게 지정할 수 있습니다. 어떻게 할 수 있습니까? 이런 식으로 시험해 보았습니다.

HTML :

<ul> 
    <li class="alert alert-{{bgcolor($index)}}" ng-repeat="item in todolist">  
    <span>{{item.name}}</span>  
    </li>               
</ul> 

JS :

작동합니다
+0

JS 목록 0 기반 한 인덱스를 상쇄되지는'경우 0 봤어 :.? 색상 = "성공"...' –

+0

을 절대로 마음, 그게 Plunker에서 고치지 않았어. –

답변

4

$scope.todolist = [ 
       { name: "Buy Food", done: false }, 
       { name: "Eat Food", done: false }, 
       { name: "Cook Food", done: true } 
      ]; 
$scope.bgcolor = function (i) { 
       var color = ""; 
       switch(i) 
       { 
        case 1: color="success"; 
        case 2: color = "warning"; 
        case 3: color = "danger"; 
       } 
       return color; 

      }; 
,하지만 당신은 스위치에서 break;이 누락 :

switch(i) { 
    case 1: color="success"; break; 
    case 2: color = "warning"; break; 
    case 3: color = "danger"; break; 
} 

를도 0 기반이므로 첫 번째 항목은이 클래스를 가져 오지 않습니다.

+0

고마워. 그랬다. – Ruby

+0

@Ruby -이게 너에게 도움이된다면 답을 받아 들일 수 있도록 표시해라. – tymeJV

+0

나는 심지어 내가 새로운 대답을 할 때까지 체크하지 않았다. [plunker] (http://plnkr.co/edit/qgmOR51tusCBTWLaDs2l)에서이 정확한 작업을 수행했습니다. +1. –

2
you can try something like this 
<ul> 
    <li class="alert alert-{{bgcolor($index)}}" ng-repeat="item in todolist">  
    <span>{{item.name}}</span>  
    </li>               
</ul> 

JS: 

    $scope.todolist = [ 
        { name: "Buy Food", done: false }, 
        { name: "Eat Food", done: false }, 
        { name: "Cook Food", done: true } 
       ]; 
    $scope.bgcolor = function (i) { 
        var color = ['success','warning','danger']; 

        return color[index]; 

       };