Closed felixunivers closed 5 years ago
If you would console.log
out the incoming changes, do you see them? Or is it only the view that is not updating?
A cause of this could be the change detection, we use OnPush
because we're NgRx.
changeDetection: ChangeDetectionStrategy.OnPush,
@felixunivers as @timdeschryver said, setting component properties after async processing will not refresh the component if the component is using ChangeDetectionStrategy.OnPush
, for such a component it would make sense to remove this and use default change detection strategy.
Another solution would be to inject change detector reference and call .markForChangeDetection()
every time data arrives.
Would you please provide a piece of code sample (something that may fit the above scenario) on how to properly use the: .markForChangeDetection()
Here is what I did and it appears working - please comment if any issues with this solution.
import { ChangeDetectorRef } from '@angular/core';
...
constructor(private cdRef: ChangeDetectorRef, .....
...
private onSocketNewData(wsData) { // triggered whenever there is a message from the server.
this.cdRef.markForCheck();
this.conStatus = 'Connected';
....
Minimal reproduction of the bug with instructions:
I have a sample WebSocket component that works well outside the 'starter' project. If I bring the component to the 'starter' project, the properties/variables interpolated with the view/template are not updated as expected after the WebSocket observable is triggered on new data.
Here are the key parts of the code:
file: websocket.service.ts
` import { Injectable } from '@angular/core'; import { WebSocketSubject } from 'rxjs/webSocket';
`
file: websocket.component.ts ( not complete - relevant parts only)
file: websocket.component.html
Expected behavior:
It is expected that after the function: onSocketNewData(msg) gets triggered by each server response the updated properties/variables (such as: conStatus, dataInBlock, etc. ) associated/interpolated with the view get updated/rendered. This works well if the component is is in its own project, but not when it becomes part of the 'starter' project.
Other information:
I would be willing to submit a PR to fix this issue:
[ ] Yes (Assistance is provided if you need help submitting a pull request)
[ ] No