What is the best way to conditionally apply a class?

后端 未结 22 2606
感动是毒
感动是毒 2020-11-22 09:12

Lets say you have an array that is rendered in a ul with an li for each element and a property on the controller called selectedIndex.

22条回答
  •  轮回少年
    2020-11-22 09:43

    If you having a common class that is applied to many elements you can create a custom directive that will add that class like ng-show/ng-hide.

    This directive will add the class 'active' to the button if it's clicked

    module.directive('ngActive',  ['$animate', function($animate) {
      return function(scope, element, attr) {
        scope.$watch(attr.ngActive, function ngActiveWatchAction(value){
          $animate[value ? 'addClass' : 'removeClass'](element, 'active');
        });
      };
    }]);
    

    More info

提交回复
热议问题