1

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>

2
  • Small Details When the div is being clicked the states of the above two classes div0f and div0g interchanges for the particular div Commented Jul 14, 2015 at 6:21
  • the active one becomes inactive and vice -versa Commented Jul 14, 2015 at 6:21

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

5 Comments

thanks a lot !!!, It worked It just that Being a newBie in angularjs , I was not been able to render this mistake
But this has One problem that It changes only one class but doesn't affect the other one on click
What do you mean "affect the other one on click"?
When I click on the Div with the class Div0f It changes to the Div0g, But I click on the Div with the Class Div0g It doesnt changes it active class
thanks With some of the minor changes Dfsq Your code began to work as per my requirement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.