harish kumar
harish kumar

Reputation: 15

Changing class in the ng-repeat using the index value

Here I have a div in the ng-repeat

<div   ng-class="{div0f:result.subscribed==omega.harish,div0g:!result.subscribed==omega.harish}" id="mecota0f" 
          ng-click="omega.harish=!result.subscribed" >

Now here result.subscribed is a boolean value coming from the services and omega.harish is my simple boolean variable the class of this div will be according to the result.subscribed value

Now I also want to change the active class of the one of the div created in the ng-repeat but the class of other created divs are kept getting affected.

var app = angular.module('subscribeWho', ['ngResource']);
app.controller('mysubscribeWho', ['subscriberFactory',
  function(subscriberFactory) {

    var self = this;


    subscriberFactory.get({}, function(subscriberFactory) {
      self.subscriberdata = subscriberFactory.user;
    });


    self.harish = false;
  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div id="border-forpb" class="col-sm-12 panel panel-default" ng-app="subscribeWho">
  <div class="panel-body" id="artle_panel_body" ng-controller="mysubscribeWho as omega">
    <div ng-repeat="result in omega.subscriberdata">
      <div ng-class="{div0f:result.subscribed==omega.harish,div0g:!result.subscribed==omega.harish}" id="mecota0f" ng-click="omega.harish=!result.subscribed">
      </div>
    </div>
  </div>

Upvotes: 1

Views: 218

Answers (1)

dfsq
dfsq

Reputation: 193311

You are changing outer scope omega.harish value which is common for all items in the ngRepeat. Instead create local scope copy of omega.harish with ngInit directive:

<div ng-repeat="result in omega.subscriberdata">
    <div ng-class="{div0f:result.subscribed==harish,div0g:!result.subscribed==harish}" id="mecota0f" 
        ng-init="harish = omega.harish"
        ng-click="harish=!result.subscribed">
    </div>
</div>

Upvotes: 1

Related Questions