admin管理员组文章数量:1410737
I'm trying to use CSS3 animation in my template, but I'm trying to find a away to add and remove class using (click)='function()'
like jQuery way.
Question is how can I do the function below which is a jQuery in Angular 2?
This is what I have at the moment in my SharedService
. I believe that you can't use Renderer
angular core ponent in the service. Reason this is in a service is so other ponents can use it.
openBasketView() {
$('#basketView').removeClass('fadeOutRight');
$('#basketView').addClass('fadeInRight');
}
closeBasketView() {
$('#basketView').removeClass('fadeInRight');
$('#basketView').addClass('fadeOutRight');
}
Rather than using something like -- I like using function
in (click)
event is better and readable as all logic is in a block function.
ponent boolean
public showThis: Boolean = false;
template view
<div class="basket animated" [ngClass]="{'myClass': showThis}"> </div>
<button (click)="showThis = !showThis"></button>
I'm trying to use CSS3 animation in my template, but I'm trying to find a away to add and remove class using (click)='function()'
like jQuery way.
Question is how can I do the function below which is a jQuery in Angular 2?
This is what I have at the moment in my SharedService
. I believe that you can't use Renderer
angular core ponent in the service. Reason this is in a service is so other ponents can use it.
openBasketView() {
$('#basketView').removeClass('fadeOutRight');
$('#basketView').addClass('fadeInRight');
}
closeBasketView() {
$('#basketView').removeClass('fadeInRight');
$('#basketView').addClass('fadeOutRight');
}
Rather than using something like -- I like using function
in (click)
event is better and readable as all logic is in a block function.
ponent boolean
public showThis: Boolean = false;
template view
<div class="basket animated" [ngClass]="{'myClass': showThis}"> </div>
<button (click)="showThis = !showThis"></button>
Share
Improve this question
edited Apr 17, 2017 at 21:13
MrNew
asked Apr 17, 2017 at 17:47
MrNewMrNew
1,4246 gold badges22 silver badges48 bronze badges
3 Answers
Reset to default 2If the display logic is in a service injected in the ponent, you can call the service from the template to add the toggled class. Additionally, if you want to turn on block display after clicking the button, you can put that logic in the ponent or in the service (in the example below, that code is in the ponent).
The ponent:
class MyComponent {
private displayBlock = false;
private turnOnBlockDisplay() {
this.displayBlock = true;
}
private getBlockClass() {
return this.displayBlock ? "block" : "";
}
constructor (private myService: ToggleClassService, ...) {
...
}
...
}
The service:
class ToggleClassService {
private fadeIn = false;
public toggleClass() {
this.fadeIn = !this.fadeIn;
}
public getClass() {
return this.fadeIn ? "fadeInRight" : "fadeOutRight";
}
}
The template:
<div [ngClass]="['basket', 'animated', myService.getClass(), getBlockClass()]">...</div>
<button (click)="myService.toggleClass(); turnOnBlockDisplay();">Toggle</button>
The CSS:
.basket {
display: inline-block;
...
}
.basket.fadeInRight {
background-color: red;
}
.basket.fadeOutRight {
background-color: blue;
}
.basket.block {
display: block;
}
You can test the code in this codepen (note: the "service" is not injected).
Alternative Way not use ts side varible use function
<li class="mobile-links__item" #childCatLi>
<div class="mobile-links__item-title">
<a routerLink="{{cat.link.link}}" class="mobile-links__item-link">{{cat.category.name}}</a>
<button class="mobile-links__item-toggle" type="button (click)="toggleClass(childCatLi)">
<svg class="mobile-links__item arrow" width="12px" height="7px"><use xlink:href="./assets/images/sprite.svg#arrow-rounded-down-12x7"></use></svg>
</button>
</div>
</li>
ponent.ts side in toogleClass
toggleClass(elem:HTMLElement){
elem.classList.toggle('mobile-links__item--open');
}
You should be using the below code
<div class="basket" [class.fadeOutRight]="showThis" [class.fadeInRight]="!showThis">
<div (click)="showThis = !showThis"></div>
Update 1 :
<div class="basket" [ngClass]="getClass()">
getClass() {
if(this.showThis)
return 'fadeOutRight';
else
retun 'fadeInRight'
}
<div [ngClass]="{ 'c1, c2, c3' : true, 'fadeOutRight' : showThis, 'fadeInRight' : !showThis }
Update 2:
Have the classes in an array as
classes:string[] = ['basket', 'animated']
<div [ngClass]="classes></div>
<button (click)="toggleClass()"></button>
toggleClass(){
this.classes.splice(this.classes.length-1,1); // removes the class
this.show=!this.show;
if(this.show) {
this.classes.push('fadeOutRight')
}else {
this.classes.push('fadeInRight')
}
LIVE DEMO
本文标签: javascriptAdding and removing class click function in Angular 2 ServiceStack Overflow
版权声明:本文标题:javascript - Adding and removing class click function in Angular 2 Service - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744256218a2597493.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论