简体   繁体   English

用ng-click链接Angular js的活动类?

[英]Active class on links angular js with ng-click?

I have a menu with links that are loaded from a json file. 我有一个菜单,其中包含从json文件加载的链接。 I am wondering what is the best way to add an active class when the user clicks the link. 我想知道当用户单击链接时添加活动类的最佳方法是什么。 I have seen examples with unordered lists, but none with actual links. 我看到了无序列表的示例,但没有实际链接的示例。 I have tried it with ng-click on the anchor tag, but it doesnt work. 我已经尝试过用ng-click锚标记单击它,但是它不起作用。 Here is the code that I have so far. 这是我到目前为止的代码。 What do you suggest is the best way to implement this? 您认为实现此目标的最佳方法是什么?

Menu.html 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 controller.js

$scope.selectedIndex = 0;

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

I think in this case you can change your itemClicked function to: 我认为在这种情况下,您可以将itemClicked函数更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM