简体   繁体   English

Angular 2+添加或删除属于另一个组件的元素类

[英]Angular 2+ Add or Remove a Class of an element that belongs to another component

I am using Angular 4 on an application where I have 2 components ... app.component and other.component 我在一个应用程序上使用Angular 4,我有2个组件... app.component和other.component

On app.component.html I have a div with a class called myClass ... app.component.html上我有一个名为myClass的类的div ...

<div class="myClass"></div>

On other.component.ts I currently have: other.component.ts我目前有:

ngOnInit() {

 jQuery('.myClass').removeClass();
 jQuery('.myClass').addClass('someClassName');

}

I want to do this the angular way instead of using jQuery. 我想以角度方式而不是使用jQuery。

My question is...How do I do this same thing with Angular 2+ ? 我的问题是......我如何使用Angular 2+做同样的事情?

If OtherComponent is on another route than AppComponent , then you could use a service to do that : 如果OtherComponent是比另一种途径AppComponent ,那么你可以使用一个服务做到这一点:

app.component.html app.component.html

<div class="myClass" #concernedDiv></div>

app.component.ts app.component.ts

@ViewChild('concernedDiv') concernedDiv: ElementRef;

constructor(private myService; MyService) {}

ngOnInit() {
  this.myService.onOtherInit.subscribe(event => {
    this.concernedDiv.className = this.concernedDiv.replace('myClass', '').trim();
  });
}

my-service.service.ts 我-service.service.ts

private sub: Subject<any> = new Subject();
public onOtherInit: Observable<any> = this.sub.asObservable();

emitEvent() { this.sub.next(/* anything you want */); }

other.component.ts other.component.ts

constructor(private myService: MyService) {}
ngOnInit() { this.myService.emitEvent(); }

By using ngClass you can use statements to add / remove a class 通过使用ngClass您可以使用语句来添加/删除类

In your component: 在您的组件中:

yourBooleanValue: boolean;

ngOnInit() {
    this.yourBooleanValue = true;
}

In html: 在html中:

<div [ngClass]="{ 'someClassName': yourBooleanValue }"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Angular 2+中的另一个列表元素之前动态添加组件 - How to dynamically add component BEFORE another list element in Angular 2+ 从Angular 2+中的同级组件访问元素 - Accessing an element from sibling component in Angular 2+ 如何通过Angular将鼠标悬停在另一个元素上时添加和删除一个元素的类? - How to add and remove class of one element while hovering over another element via Angular? ReactJS onclick 添加或删除 class 到另一个元素 - ReactJS onclick add or remove class to another element Angular 2+ 测试组件 - Angular 2+ testing a component 角度:检查元素是否具有一个类,然后将一个类添加到另一个元素 - Angular: Check if element has a class and add a class to another element 在Angular 2+中使用包含时获得对组件的类/构造函数的引用 - Getting a reference to the class/constructor of an component when using transclusion in Angular 2+ 单击一个元素从另一个元素添加/删除类 - Click one element add/remove class from another element AngularJS指令在点击时添加类,但如果单击则删除并将其添加到另一个元素 - AngularJS Directive to add class on click, but remove and add it to another element if clicked 在项目选择上添加 class 并将其删除以添加到另一个元素选择上 - Add a class on item selection and remove it to add on another element selection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM