2016-03-01 61 views
0

我试图改变父类的名字与不同的孩子在这里NG单击 是代码:父纳克级的NG-点击angularjs

<div class="item" 
      ng-class="{'_first': isFirst, '_second': isSecond}" 
      ng-repeat="item in currentCtrl.currentData"> 
<div class="btn-container"> 
       <button class="btn" 
         ng-click="isFirst = !isFirst">first</button> 
      </div> 
      <div class="btn-container"> 
       <button class="btn" 
         ng-click="isSecond = !isSecond">second</button> 
      </div> 
</div> 

它的工作的第一只 我错过?

编辑: 发现故障,代码是正确的

+0

您有'_first'和'_second'定义的CSS类? 'isSecond'是否可以工作,并且在你首先点击时保持这种状态? – jcc

+0

@jcc麻烦的是,'isFirst'工作,它是添加类的项目,'isSecond'不,我不明白这个问题 – FrontEndist

+0

我问这是否,当你第一次加载页面,如果你点击首先是“isSecond”,它起作用了吗?我认为你的'ng-click'表达式不会关闭其他属性,从而产生一个导致没有任何事情发生的冲突。 – jcc

回答

1

你有这样吗?

var app = angular.module("app",[]); 
 
app.controller("ctrl", function($scope){ 
 
    
 
    $scope.items = [1,2,3,4]; 
 
    $scope.isSecond = true; 
 
    $scope.isFirst = false; 
 
    
 
    
 
    
 
    })
._first{ 
 
    color:red; 
 
    } 
 

 
._second{ 
 
color:blue; 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<div ng-app="app" ng-controller="ctrl"> 
 

 
     <div class="item" ng-class="isFirst== true ? '_first' : '_second'" 
 
     ng-repeat="item in items"> 
 
     <p> this is a test</p> 
 
      <div class="btn-container"> 
 
       <button class="btn" 
 
         ng-click="isFirst= !isFirst">first</button> 
 
      </div> 
 
      <div class="btn-container"> 
 
       <button class="btn" 
 
         ng-click="isFirst = !isFirst">second</button> 
 
      </div> 
 
     </div> 
 
    
 
</div>