UPDATE: I have read that in Angular, you cannot manipulate the DOM. What are you supposed to do then, what is best practice? I have tried using a timeout, NgZone, ChangeDetectionStrategy.OnPush, ChangeDetectorRef detectChanges() all with no success.
I have an html img element:
<img class="selected" id="icon-image-{{item.id}}" src="{{item.icon}}" height="75" width="75" />
and some sccs:
img.selected {
-webkit-filter: grayscale(100%); /* Chrome, Safari, Opera */
filter: grayscale(100%);
}
When this loads, it applies the selected class, and makes the image grey as expected.
However, when I run the following typescrpt the image just stays grey:
let imgEl: HTMLElement = document.getElementById('icon-image-' + subCategoryModel.id);
imgEl.className = "";
When I log the className, it shows selected is there before, and has been removed after.
console.log(imgEl.classList);
Any ideas why, even though I remove a className, the style is not reflected?
When I look at the source code, it is not updating the DOM, but imgEl.classList is being changed. Do you know why?
Thanks
UPDATE:
Something strange, if I change the className in the DOM manually (in Firebug for example), it reflects the change. However, as soon as I click the image, it updates to what the original value was. It is as if the DOM is being refreshed.
If this is the case, then how is it possible to update the DOM dynamically? Because the DOM refresh just overwrites all changes.
Html
<ion-row *ngFor="let trio of getTriples()">
<ion-col *ngFor="let item of trio" (click)="itemTapped(item)">
<center>
<div class="row responsive-md">
<img class="selected" id="icon-image-{{item.id}}" src="{{item.icon}}" height="75" width="75" />
</div>
</center>
</ion-col>
</ion-row>
Typescript
itemTapped(subCategoryModel: SubCategoryModel) {
let idx: number = this.isInArray(this.employeeModel.subCategories, subCategoryModel);
let imgEl: HTMLElement = document.getElementById('icon-image-' + subCategoryModel.id);
if (idx >= 0) {
this.employeeModel.subCategories.splice(idx, 1);
imgEl.setAttribute("class", "");
} else {
this.employeeModel.subCategories.push(subCategoryModel);
imgEl.setAttribute("class", "selected");
}
console.log('icon-image-'+subCategoryModel.id+' ('+imgEl.id+'): ' + document.getElementById('icon-image-' + subCategoryModel.id).className);
}