无法将jquery代码转换为angularjs,需要使用angularjs切换类

not able to covert jquery code into angularjs, need to toggle class using angularjs

本文关键字:angularjs jquery 代码 转换      更新时间:2023-09-26

我想把下面的jquery代码转换成angularjs..请帮助。

$('.favorite').click(function() 
{
  $(this).toggleClass('active');
});
<span ng-click="toggle()" ng-class="{active: isShown}">look at my class (working)</span><br>

在你的控制器

$scope.toggle = function () {
      $scope.isShown = !$scope.isShown
}

你不能这样做:

<button class="favorite" ng-click="state = !state" ng-class="{'active' : state}">Some text...</button>