gpt4 book ai didi

javascript - 使用 jquery 将类添加到 ng-repeat 中的第一个项目

转载 作者:数据小太阳 更新时间:2023-10-29 03:58:52 24 4
gpt4 key购买 nike

我是复杂指令的新手,我正在尝试使用 jquery 向 ng-repeat 中的第一项添加一个类(不幸的是我的项目正在使用它),就像在指令的 Controller 中一样:

   var highlightFirst = function(){
$('.pointer').find('.fa-angle-down-first').next().addClass('boldit');
console.log('in here')
}

highlightFirst();

//also tried this:
angular.element(document).ready(function(){
$('.pointer').find('.fa-angle-down-first').next().addClass('boldit');
console.log('in here')
});

我之后立即调用该函数。什么都没发生。

当我运行 $('.pointer').find('.fa-angle-down-first').next().addClass('boldit');在 chrome 开发工具中,我想要加粗的内容实际上是加粗的......我缺少什么才能让这个特定的解决方案正常工作?执行此操作的最佳 Angular 方法是什么?

最佳答案

你实际上并不需要 jquery 并且应该尽可能避免在 Angular 应用程序中使用 jquery,你应该使用 ng-class$first这是工作 demo

<ul>
<li ng-repeat="item in items" ng-class="{'fooClass': $first}">{{ item }}</li>
</ul>

$first 的好处是,当您对 ng-repeat 进行排序时,它仍然可以正常工作。

关于javascript - 使用 jquery 将类添加到 ng-repeat 中的第一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35249245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com