1

Suppose I have a component html where there is my html my progress bar

 <round-progress #progress
    [current]="current"
</round-progress>

Current is the percentage value. In my ts component I do:

this.percentageSubscription = this.percentageService.percentage$.subscribe((percentage) => {
     this.current = this.current + percentage;
     console.log(this.current);
});

The value that I print is correct. It is increase following another algorithm. The console.log show the correct increment but my percentage value is not increase in the progress bar.

Can anyone help me?

2
  • How does the round-progress component use the current value? What does it do with it? Try adding {{current}} to your template and see if that value updates. If so, the round-progress component most likely simply only reads the current value once. Commented Nov 21, 2018 at 15:09
  • @Arne i put the {{current}} and the value is not update everytime that the percentage is increase but it show at 0 value and 100 value but it doesn't show 20% or 35%. I don't know what I have to do to update this round-progress Commented Nov 21, 2018 at 15:18

1 Answer 1

2

Since your value does update correctly in your subscription, but not in your template, the change detection fails to register the new value. You could manually trigger change detection, but you shouldn't.

I'd suggest using the async pipe. And thus rewriting your usage to:

<round-progress #progress
    [current]="current$ | async"
</round-progress>

And:

this.current$ = this.percentageService.percentage$.pipe(
  scan((acc = 0, percentage) => acc + percentage)    
);

As a bonus you don't have to manage your subscription no more. If you locally need the "current" for other purposes you could either share the observable (and have to manage subscriptions again) or you could add a tap that sets a local value.

Sign up to request clarification or add additional context in comments.

4 Comments

your solution is not work! :( the value is not update. Now i'm tring to put markForCheck and detectionChanges after subscrive value but they doesn't work same!
If you add a tap(value => console.log('my new value: ', value) under the scan (second parameter of the pipe), you see the value updating? If you in your template add {{current$ | async}} that value does not update?
yeah in console.log the value is update but in the html isn't update in correct way!
I’m really curious what you’ve been up to in that component and what the rest of the code looks like. The async pipe even works when you’d completely disable the angular change detection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.