angular-bootstrap-switch icon indicating copy to clipboard operation
angular-bootstrap-switch copied to clipboard

A switch that is not visible on page load will not be properly initialized

Open Splaktar opened this issue 11 years ago • 16 comments

Using: bootstrap-switch: v3.0.2 angular-bootstrap-switch: v0.4.0-alpha.1 - 2014-11-21 angularjs: 1.3.6

If the switch is within a tab that isn't initially visible, then it will not be initialized properly with the value of its ng-model. This is also true with other components that contain/hide the switch like angular-wizard, angular-material tabs (md-tab), etc.

The scope.$watch(attrs.ngModel, function (newValue) { is called properly and sets the state on the element. Later when the element becomes visible, the value is correct but the display state is not.

We're currently working around the issue by setting up an interval which polls to see when the element becomes visible and then makes the call to:

element.bootstrapSwitch('state', newValue === getTrueValue(), true);

This seems to set both the value and display state properly so that they stay in sync, There is likely a better solution.

This does not appear to be covered by an existing test since I was able to clone the project and run the Karma tests with AngularJS 1.3.6. They all passed.

Splaktar avatar Dec 16 '14 15:12 Splaktar

Could you please do 2 things?

  1. Try out the latest develop branch of angular-bootstrap-switch
  2. Reference a plunker where the issue is clear

Thanks.

frapontillo avatar Dec 16 '14 15:12 frapontillo

Verified that it is reproducible with latest develop version of angular-bootstrap-switch. Working on a plunker.

Splaktar avatar Dec 16 '14 17:12 Splaktar

Plunker here: http://plnkr.co/edit/pyAhiI?p=preview

Splaktar avatar Dec 16 '14 18:12 Splaktar

+1 I have the same issue.

burakkilic avatar Dec 24 '14 21:12 burakkilic

Setting up an interval to solve this seems pretty hackish... Do you guys feel like this is an AngularJS issue? It surely looks like that to me.

frapontillo avatar Jan 19 '15 08:01 frapontillo

I don't think this is an AngularJS issue because I am also experimenting with another 3rd party switch widget, and it is not having this problem.

dlwhiteman avatar Jan 19 '15 16:01 dlwhiteman

Another fix that I had to do for an internal directive was to put a $scope.watch on the isolated scope attributes coming into the directive. AngularJS wasn't keeping them in sync with the outside scope changes. Using the watch allowed late changes to the outside model to be reflected within the directive.

Either this is a bug in AngularJS or a new requirement for how isolated scope is supposed to work. I haven't figured out which yet.

Splaktar avatar Jan 22 '15 17:01 Splaktar

Has there been any progress on this? I'm facing the same issue.

treyhoover avatar Mar 23 '15 21:03 treyhoover

I haven't had any time to work on this, sadly. I will gladly accept PRs that do not make use of polling or any terrible hacky workaround.

frapontillo avatar Mar 23 '15 21:03 frapontillo

Actually, it seems to be fixed for me in v0.4.0-alpha.1.

treyhoover avatar Mar 23 '15 22:03 treyhoover

it happened to me using the non angular bootstrap switch and quite funny how I stumbled across this thread randomly considering ive been using both. it appears it is a bug in bootstrap switch library IMO.

i worked around by initing the switch upon showing the tab - best I could do for now.

j-r-t avatar Apr 07 '15 16:04 j-r-t

@jessertaylor are you able to put up an issue on the bootstrap-switch repository?

frapontillo avatar Apr 27 '15 14:04 frapontillo

@frapontillo will do

j-r-t avatar Apr 27 '15 14:04 j-r-t

Any update on this?

leewisestamp avatar Mar 01 '16 09:03 leewisestamp

@leewisestamp the parent bug hasn't been fixed yet.

frapontillo avatar Mar 01 '16 10:03 frapontillo

Someone got something to fix this issue?

albertjose avatar Jun 14 '16 14:06 albertjose