question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Start and end handles not updating when ngModel changes

See original GitHub issue

Hi,

I’m experiencing strange behaviour when using dynamic values for the ngModel. When the values of ngModel change, it changes them in the slider, but right after it changes them back to the initial values.

Because the values in the ngModel are dynamic for me, and are not defined right away, I manually initialize it with [ 0, 1 ]. This works and the slider gets updated. However, when I update these to [1950, 2017] it updates the slider, but instantly sets it back to [0,1].

The [min] and [max] are working fine for me, they are updated at the same time as the ngModel.

So I don’t know If I’m completely messing something up. Anyway, I’ve found a (temporary) solution that seems to work just fine for me. Maybe this helps to highlight what I’m struggling with at the moment.

I’ve updated ngOnChanges as such:

ngOnChanges(changes: any) {
    if (this.slider && (changes.min || changes.max || changes.step)) {
        setTimeout(() => {
            this.slider.updateOptions({
                range: {
                    min: this.min,
                    max: this.max
                },
                step: this.step
            });
        });
    }

    if(this.slider && changes.ngModel) {
        setTimeout(() => {
            this.slider.set(this.ngModel);
        });
    }
}

I’ve also updated the [(ngModel)] attribute to [ngModel] and that seems to fix the problem. I manually catch any changes through (change).

Without these changes, the values in my view will be the correct ones, but the slider will have both handles at the beginning. I don’t know if this helps to clarify what I’m doing (or doing wrong), but if you have any questions, I’m more than happy to answer them.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

2reactions
ClientsideDesigncommented, Jul 17, 2018

@roelofjan-elsinga I was having a similar problem generating sliders dynamically with ngFor, but having all config in an object in my .ts file rather than inline seems to have solved the issue. All my ngmodel values are stored in an object under the corresponding node name, so the template code is just:

<nouislider [config]="sliderConfig[node.name]" (ngModel)="slider_ngModel[node.name]"></nouislider>

Then in my .ts file:

slider_ngModel = {};

and

sliderConfig: any = { slider_1_name: { connect: true, start: [20, 220], step: 1, tooltips:true, range: { min: 20, max: 220 }, }, slider_2_name: { connect: true, start: [1900, 2018], step: 1, tooltips:true, range: { min: 1900, max: 2018 } } };

Hope that helps.

0reactions
Suyashjccommented, Feb 28, 2019

slider_ngModel[node.name]

I have a very large array list hard coding sliderConfig will be difficult. Is there any other way?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 2 - [(ngModel)] not updating after [value] changes
1. The ngModelChange thing doesn't work either. It's really weird because the value in the input GETS UPDATED, but not the expression {{product....
Read more >
Difference between (change) and (ngModelChange) in Angular
In this post we're going to cover the difference between (change) and (ngModelChange) events with an inside an Angular component.
Read more >
Keeping Data in Sync with Two-way Binding Using Ngmodel
Syncing data between the control and the UI is essential to create an excellent user experience and easily handle form data.
Read more >
select - AngularJS: API
If the viewValue of ngModel does not match any of the options, then the control will automatically add an "unknown" option, which it...
Read more >
Top 18 Most Common AngularJS Developer Mistakes - Toptal
It is not difficult to start using, but the goals that it is intended to ... The front-end developer needs to handle XHRs,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found