TL;DR: ngOnChanges shows that changes are being detected on an input property, but the view is not being updated
I'm working on an Angular (2+) app trying to make a progress bar for an asynchronous task being fulfilled by a service using an Observable. The Observable pushes numbers as data to indicate what percent of the task has been completed. There are three pieces involved: The Service, the ResultsComponent, and the ProgressBarComponent (child of ResultsComponent) The basic data flow is:
- Number pushed from the Observable to ResultsComponent
- ResultsComponent.percentLoaded is set to equal this number
- ProgressBarComponent uses percentLoaded as an Input and view is updated
The problem is that the view isn't actually being updated.
In ResultsComponent I have this block:
this.service.doTheTask()
.subscribe(
data => {
this.percentLoaded = data;
this.ref.detectChanges();
},
e => console.log('error:', e),
() => {
this.isLoading = false;
this.percentLoaded = 0;
}
);
Adding ref.detectChanges() successfully makes the OnChanges hook fire in ProgressBarComponent. Component and template are shown below:
Component
export class ProgressBarComponent implements OnChanges {
@Input() percentLoaded = 0;
constructor() { }
ngOnChanges(changes) {
console.log('changes: ', changes);
}
}
Template
<div class="meter">
<div class="percent" [style.width.%]="percentLoaded"></div>
</div>
<p>{{percentLoaded}}% Loaded</p>
As you can see, I'm just logging changes to test. I've verified that ngOnChanges is firing, and that the values are correct. Everything I've read says "Once changes are detected, the view automatically updates" so I don't know what else to do.
Any suggestions?