你如何从Angular的一个元素中获得一个类?

Max*_*ynn 2 javascript jquery angularjs

<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');
};
Run Code Online (Sandbox Code Playgroud)

我正在尝试获取当前的类,以便我可以获得上面的类来添加对它开放的类.

做这个的最好方式是什么?

因此,.js-show-more有类.open类时.js-show-more-toggle被点击.我需要这个来处理这段代码的重复.

Cor*_*lva 5

您应该根据变量知道该类.

<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>
Run Code Online (Sandbox Code Playgroud)


如果使用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>
Run Code Online (Sandbox Code Playgroud)

只需扩展每个隔离范围!