使用索引值

时间:2015-07-14 06:14:18

标签: angularjs angularjs-ng-repeat angular-ng-class

这里我在ng-repeat中有一个div

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

现在这里result.subscribed是一个来自服务的布尔值,而omega.harish是我的简单布尔变量 这个div的类将根据result.subscribed值

现在我还想更改ng-repeat中创建的div之一的活动类,但其他创建的div的类会受到影响。

&#13;
&#13;
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;
  }
]);
&#13;
<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您正在更改外部范围omega.harish值,这对于ngRepeat中的所有项都是通用的。而是使用omega.harish指令创建ngInit的本地范围副本:

<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>