【发布时间】:2018-08-02 04:21:48
【问题描述】:
我有一个列表和我使用的插件 (dragula),它在某些动作上添加了某些 CSS 类。我正在使用 Angular 5。我想找出某个类 (myClass) 的存在并将其删除并替换为 (yourClass)。在 jQuery 中我们可以这样做
$( "p" ).removeClass( "myClass" ).addClass( "yourClass" );
如何在 Angular5 中实现这一点。这里的主要问题是myClass 被插件自动添加到选定的li 中。所以使用函数我无法设置类。
当我尝试使用 renderer2 时,它正在删除 CSS 类并添加另一个类。但它只添加到第一个li。我的代码是:
let myTag ;
myTag = this.el.nativeElement.querySelector("li");
this.renderer.addClass(myTag, 'gu-mirrorss')
this.renderer.removeClass(myTag, 'dragbox');
<div class="right-height" id ='dest' [dragula]='"second-bag"' [dragulaModel]="questions">
{{ questions.length == 0 ? ' Drag and drop questions here ' : ' '}}
<li #vc data-toggle="tooltip" data-placement="bottom" title= {{question.questionSentence}} class="well dragbox" *ngFor="let question of questions; let i = index" [attr.data-id]="question.questionId" [attr.data-index]="i" (click)="addThisQuestionToArray(question,i, $event)" [class.active]="isQuestionSelected(question)" #myId > {{question.questionId}} {{question.questionSentence}}</li>
</div>
【问题讨论】:
-
在“角度方式”中,您使用 [ngClass]="{'class1':condition1,'class2':condition2}"
-
你想在打字稿中添加/删除吗?
-
@SandipPatel 是的。我认为 [ngClass]="'class1':condition1,'class2':condition2}" 是不可能的,因为列表会动态变化,并且该类是由插件添加的,我希望它在特定条件下删除
-
[ngClass] 在这种情况下不起作用,因为插件添加了类。您需要使用 ElementRef 找到它并手动将其删除。
标签: css angular typescript