<div class="js-show-more">
<p class="app-light-text">
The factors of a number are all the numbers that divide into it. The highest common factor, HCF, of two
or more numbers is the highest factor that divides into all the numbers. For example, what is the highest
common factor of 24 and 36? The answer is 12. 12 is a factor of both 24 and 36 and it is the highest
factor that they have in factors of a number are all the numbers that divide into it. The highest common factor, HCF, of two
or more numbers is the highest factor that divides into all the numbers. For example, what is the highest
common factor of 24 and 36? The answer is 12. 12 is a factor of both 24 and 36 and it is the highest
factor that they have in common
</p>
<a ng-click="toggle($event)" class="js-show-more-toggle"></a>
</div>
// JS
scope.toggle = function (e) {
console.log(scope);
// $(e).parents('.js-show-more').toggleClass('open');
};
我正在尝试获取当前类(class),这样我就可以获取上面的类(class)以将类(class)添加到它。
执行此操作的最佳方法是什么?
所以当类 .js-show-more-toggle
被点击时,.js-show-more
有类 .open
。我需要它来处理这段代码的副本。
最佳答案
你应该知道基于变量的类。
<div class="js-show-more" ng-class="{open:isOpen}>
<p class="app-light-text">...</p>
<a ng-click="isOpen = !isOpen" class="js-show-more-toggle"></a>
</div>
如果使用
ng-repeat
它会变得更加容易,因为 ng-repeat
会自动创建隔离范围。请参见下面的示例:
<div class="js-show-more" ng-repeat="item in items" ng-class="{open:item.isOpen}">
<p class="app-light-text">...</p>
<a ng-click="item.isOpen = !item.isOpen" class="js-show-more-toggle"></a>
</div>
简单地扩展每个隔离范围!
关于javascript - 你如何从 Angular 中的元素中获取类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39082505/