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

时间:2016-08-22 14:44:59

标签: 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');
};

我试图获取当前的类,以便我可以获得上面的类来添加对它开放的类。

这样做的最佳方式是什么?

点击课程.js-show-more时,.open.js-show-more-toggle个班级。我需要这个来处理这段代码的重复。

2 个答案:

答案 0 :(得分: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>

<小时/> 如果使用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>

只需扩展每个隔离范围!

答案 1 :(得分:1)

您可以 使用jQuery,因为这就是您正在使用的内容:

var className = $('js-show-more').attr('class');