Serhiy Serhiy - 1 year ago 68
AngularJS Question

How to change background color of an item based on the click in AngularJs

I have listed a number of 's using ng-repeat.

When I click on one (div1) background color becomes blue, and if I click on other div (div2) - div1-background-color becomes white as it was at the begging and div2-background-color becomes blue.
here is my html:

<div ng-repeat="folder in vm.folders track by $index" >

Answer Source

var app = angular.module("ap",[]);

   $scope.changeIndex = function(index){
    $scope.selected = index;
  color: blue;
<script src=""></script>
<body ng-app="ap" ng-controller="con">
<div ng-repeat="n in [10, 20, 30, 40] track by $index">
  <div ng-class="{blue: selected==$index}" ng-click="changeIndex($index)">{{n}}</div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download