javascript - 你如何从 Angular 中的元素中获取类?

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

我正在尝试获取当前类(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/

相关文章:

javascript - ajax后重新构建iframe

javascript - 连接来自不同服务器的 socket.io 时出错

angularjs - 嵌套指令未编译

javascript - 检查一个具有动态值的单选按钮

javascript - 使用ajax获取选项后将值设置为下拉列表

javascript - 单击行时,Angular UI 网格显示另一个 View

javascript - 目标 ="_blank"不打开新选项卡

javascript - 当使用 Javascript 填充表数据时,jQuery 数据表功能不起作用

jquery - 我可以将 CSS "theme"作为 JSON 文档存储在 CouchDB 中吗? (然后加载并应用它?)

angularjs - Angular 1.3.10 未捕获错误 : [$injector:modulerr]