如何切换ng-class

时间:2016-08-02 16:04:22

标签: javascript html css angularjs

在这个ng-repeat中,我将显示前两行。当我点击显示更多如何更改css display:block 。显示更多应该切换。

JSON

"one":[{  
       "name":"Raseberry Lemon Cake"
    }]
"two":[{  
       "name":"Raseberry Lemon Cake"
    }]
"three":[{  
       "name":"Raseberry Lemon Cake"
    }]
"four":[{  
       "name":"Raseberry Lemon Cake"
    }]
"five":[{  
       "name":"Raseberry Lemon Cake"
    }]

HTML

    <div class="GridBrd" ng-repeat="Detail in Results" 
     ng-class='{showflight: $index > 1}'>  </div>

    <span class="moreOpt" ng-click="show()" 
       <a href="javascript:void(0);">Show More</a>
    </span>

CSS

.showflight{
    display:none;
 }

脚本

 $scope.show = function (){
    ***----how can I change the ng-class block and none here?----***
 }

3 个答案:

答案 0 :(得分:1)

作为替代答案,您可以在limitTo上使用ngRepeat内置过滤器。我们的想法是,您有一个切换表达式的按钮,限制在showAll为真时将切换到项目的最大长度,如果为假,则会在2上回退;

e.g。

<button ng-click="$ctrl.showAll = !$ctrl.showAll">Toggle</button>

<ul>
  <li ng-repeat="item in $ctrl.items | limitTo: ( $ctrl.showAll ? $ctrl.items.length : 2 )">{{item}}</li>
</ul>

Working demo

答案 1 :(得分:0)

<div ng-class="{ 'displayBlock' : variable,  'displayNone' : !variable }">
   ...
</div>

CSS

.displayBlock {
    display: block;
}
.displayNone {
    display: none;
}

controller.js

$scope.variable = false;
if(your condition) {
   $scope.variable = true;
}

你可以用角度来改变css类。 也许它有点矫枉过正,但我​​希望它可以提供帮助。

答案 2 :(得分:0)

试试这个:

HTML

<div class="GridBrd" ng-repeat="Detail in Results" 
 ng-class='{showflight: $index > 1 && !showAll}'>  </div>

<span class="moreOpt" ng-click="showAll = !showAll">
   <a href="javascript:void(0);">Show More</a>
</span>