Traversing the DOM: bad idea
You should not use jQuery to traverse the DOM and apply some transformations to it, here is why:
Jquery is tightly coupled with the DOM while angular tends to abstract all these interactions: when you modify the DOM in angular, you are supposed to use the Renderer API that will apply transformations on the elements. This is what allows rendering on Workers, building nativescript applications, etc.
Some problem may occur if you want to traverse the DOM from a parent component to apply modifications :
- Some elements can be added to or removed from the DOM dynamically by the Angular renderer. How to deal with elements that are rendered conditionally ? traversing on every change detection is an anti-pattern.
- How to be sure the plugin you applied on an element has been properly destroyed when the element has been removed by the angular renderer ?
Sometimes you do need jQuery
If you do need to use jQuery, for example to apply a jQuery plugin on an element, it is a better practice to write a directive or a component and apply the plugin by adding the directive or component inside your template than traversing the DOM from a component.
You can create a directive like this :
NB: I have no idea of the full calendar API, so it is full improvisation.
@Directive({ selector: '[fullCalendar]' })
export class HighlightDirective implements OnInit,OnDestroy,OnChanges {
private jqElement:any;
constructor(private el: ElementRef) {
el.nativeElement.style.backgroundColor = 'yellow';
}
@Input()
options:any={};
ngOnInit(){
this.jqElement = $(this.el.nativeElement);
this.jqElement.fullCalendar(this.options);
}
ngOnChanges(changes:{[k:string]:SimpleChange}){
if(changes['options']&&!changes['options'].firstChange) // do not apply options on first change (jquery plugin not applied)
this.jqElement.fullCalendar("option",this.options);
}
ngOnDestroy(){
this.jqElement.fullCalendar("destroy");
}
}
and apply it this way:
<div fullCalendar [options]="someOptions"></div>