I am adding a css
class to elements from a service, but the changes do not happen immediately. The changes take about a second to update the view. I need the changes to happen immediately with no delay. I have tried an ApplicaionRef and calling this.applicationRef.tick()
in the service, but that did not work. I tried running the service's method in NgZone's this.zone.run
, but that also did not work.
@Component({
selector: 'calling',
templateUrl: './calling.component.html',
styleUrls: ['./calling.component.scss']
})
export class CallingComponent {
constructor(private hidingService: HidingService) {}
callHide() {
this.hidingService.hide();
}
}
@Injectable({
providedIn: 'root'
})
export class HidingService {
constructor() {}
hide() {
const elements = document.querySelectorAll('.hideable-field');
elements.forEach(element => {
element.classList.add('hide');
});
}
}
Here's the CSS that's in styles.css (accessible from all components) for reference, but this part is working. Adding this 'hide' class hides the field with this CSS:
.hideable-field {
&.hide {
visibility: hidden !important;
}
}
If I use this hiding logic in the Component, it works. The fields are hidden immediately. If I use this hidden logic in the service it takes a moment.