如何在ng-click按钮上添加活动类?

时间:2017-01-13 10:45:47

标签: javascript html angularjs

这是按钮的html代码,taskfilter是按钮在点击时的工作方式的过滤器,类名是'sel'

<a class="clear-completed" ng-click="taskfilter = 1" ng-class="{'sel':enabled}">
  <span>show completed</span>.
</a>
<a class="clear-completed" ng-click="taskfilter = 2" ng-class="{'sel':enabled}">
  <span>show to do</span>.
</a>
<a class="clear-completed" ng-click="taskfilter = 0" ng-class="{'sel':enabled}">
  <span>show all</span>.
</a>  

这是我用来添加类范围的代码,点击按钮删除了html中的索引,因为它无法正常工作

$scope.taskfilters = 0;
$scope.taskfilter = function(index) {
    $scope.taskfilters = index;
};

4 个答案:

答案 0 :(得分:1)

您可以使用ngClass指令

  

如果表达式求值为一个对象,则对于具有truthy值的对象的每个键值对,相应的key将用作class name

<a ng-class="{'active' : taskfilter == 0}" ng-click="taskfilter = 0">

答案 1 :(得分:1)

<a class="clear-completed" ng-click="taskfilters = 1" ng-class="{'sel':taskfilters == 1}">
     <span>show completed</span>.
</a>
<a class="clear-completed" ng-click="taskfilters = 2" ng-class="{'sel':taskfilters} == 2">
     <span>show to do</span>.
</a>
<a class="clear-completed" ng-init="taskfilters = 0" ng-click="taskfilters = 0" ng-class="{'sel':taskfilters == 0}">
     <span>show all</span>.
</a>

答案 2 :(得分:1)

for path in paths:
        for root, dirs, files in os.walk(path):
            dirs = sorted(dirs)
            files = sorted(files)
            team = self.get_team(path)
            level = root.replace(path, '').count(os.sep)
            indent = ' ' * 4 * (level)
            subindent = ' ' * 4 * (level + 1)
            basename = os.path.basename(root)
            if firstrun:
                table.append('{0}<tr class="{2}"><td>{1}</td><td>{2}</td></tr>'.format(indent, basename, team))
                coretasks[basename] = len(table) - 1
                for f in files:
                    table.append('{0}<tr class="{2}"><td>{1}</td><td>{2}</td></tr>'.format(subindent, f, team))
            else:
                parsed_folders = []
                if basename in coretasks:
                    inserted_files = 0
                    for f in files:
                        table.insert(coretasks[basename] + 1, '{0}<tr class="{2}"><td>{1}</td><td>{2}</td></tr>'.format(subindent, f, team))
                        inserted_files += 1
                    parsed_folders.append(basename)
                    for coretask in coretasks.keys():
                        if not coretask in parsed_folders: coretasks[coretask] += inserted_files
        firstrun = False
    print('\n'.join(table))

答案 3 :(得分:0)

var app = angular.module("ap",[]);

app.controller("con",function($scope) {

$scope.class = "red";
$scope.changeClass = function(){
if ($scope.class === "red")
  $scope.class = "blue";
else
  $scope.class = "red";
};

});

.red{
 color:red;
 }

  .blue{
  color:blue;
 }