I am migrating an AngularJS app to Angular 2+. How should I implement the below code in Angular 2+ in order to add or remove CSS classes to the DOM:
link(scope, element, attrs) {
let app = angular.element(document.querySelector('#app'))
element.on('click', (event) => {
if (app.hasClass('nav-collapsed-min')) {
app.removeClass('nav-collapsed-min')
this.collapseService.notify(false)
} else {
app.addClass('nav-collapsed-min')
this.collapseService.notify(true)
}
return event.preventDefault()
})
}
querySelectororjQueryin your Angular 2, is not need and it might break your code if you want to use SSR later on. Take a look to this post blog.angularindepth.com/…. Anyway, most of this cases where you just want to toggle class can be done withngClassdirective