我的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.
更多推荐
发布评论