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

问题描述:

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

我想获取当前的类,以便我可以再获取上面的类来添加打开它的类。你如何从Angular的一个元素中获得一个类?

这样做的最好方法是什么?

因此.js-show-more具有类别.open当类.js-show-more-toggle被点击。我需要这个工作来重复这段代码。

你应该知道基于变量的类。

<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> 

只是扩展每个隔离范围!

+0

对于角度的方法,我认为这是正确的方法 –

+0

噢好的一个...这是一个很好的方法,我没有想到。会放弃它。 –

+0

啊,这个问题是我需要在多个元素上使用它,所以它也打开我所有的其他行。有没有办法让这个元素具体化? –

可以使用jQuery,因为这就是它看起来像你使用:

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