用ng-click链接angular js的活动类

Active class on links angular js with ng-click?

本文关键字:活动 js angular ng-click 链接      更新时间:2023-09-26

我有一个从json文件加载链接的菜单。我想知道当用户单击链接时添加活动类的最佳方法是什么。我见过无序列表的例子,但都没有实际链接。我已经尝试过它与锚标记上的ng-click,但它不起作用。这是我到目前为止的代码。你认为最好的实现方式是什么?

Menu.html

<ul ng-controller="menuList">
   <li ng-repeat="words in allTerms track by $index"  ng-class="{ 'active': $index == selectedIndex}"><a href  id="{{ $index }}" ng-click="testing($event); PracticeTerm(); itemClicked($index);">{{words.term}}</a></li>
</ul>

controller.js

$scope.selectedIndex = 0;
$scope.itemClicked = function($index) {
    console.log($index);
    $scope.selectedIndex = $scope.allTerms[$scope.shared[$scope.shared.length - 1]].term;
};

我认为在这种情况下,您可以将itemClicked函数更改为:

$scope.itemClicked = function ($index) {
  console.log($index);
  $scope.selectedIndex = $index;
};