如何在angular2中使用add class属性(How to use add class property in angular2)

编程入门 行业动态 更新时间:2024-10-27 05:29:03
如何在angular2中使用add class属性(How to use add class property in angular2)

我的angularjs 2代码有问题。我想通过angular2应用这个css。

在这里,我的jquery代码:

显示如何在角度2中使用此代码。我应该如何在角度2中使用属性添加类。

请帮帮我。

I have issue for my angularjs 2 code.I want to apply this css through angular2.

Here, my jquery code :

Show how can I use this code in angular 2. how should I use the property add class in angular 2.

Please help me.

最满意答案

不幸的是,到目前为止每个人都给你Angular答案。当使用Angular2时,尽量不要使用jQuery。 很少有jQuery应该直接使用的情况。

要有条件地将类添加到DOM元素,您需要向元素本身添加条件变量。

例如,如果您希望<input id="username" class="username">有条件地拥有slide1类,您可以将input元素更改为如下所示:

<input id="username" class="username [class.slide1]="myBoolean">

该布尔值可以通过click函数设置。 要将Click事件添加到按钮,您可以使用<input type="button" (click)="myFunction()"> 。 然后,您将在组件中定义一个名为myFunction()的函数。 myFunction()会将this.myBoolean设置为true或false,这将切换slide1类。

请参阅此处了解angular2中的事件(如单击)以及有条件地添加样式的方式 。

Unfortunate that everyone so far is giving you Angular answers.. When using Angular2, try not to use jQuery. There are very few cases where jQuery should be used directly.

To conditionally add a class to a DOM element, you need to add a conditional variable to the element itself.

For example, if you want <input id="username" class="username"> to conditionally have the slide1 class, you would change the input element to look like this:

<input id="username" class="username [class.slide1]="myBoolean">

That boolean value can be set by the click function. To add a click event to, say, a button, you would have <input type="button" (click)="myFunction()">. You would then define a function called myFunction() in the component. myFunction() would set this.myBoolean to true or false, which will toggle the slide1 class.

See here for how events (like click) in angular2 work and here for conditionally adding styles.

更多推荐

本文发布于:2023-08-06 19:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454646.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   如何在   add   class   property

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!