Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch-change="onChange()" gets called immediately when loading #120

Closed
pradt opened this issue Sep 9, 2016 · 7 comments
Closed

switch-change="onChange()" gets called immediately when loading #120

pradt opened this issue Sep 9, 2016 · 7 comments

Comments

@pradt
Copy link

pradt commented Sep 9, 2016

Hi, I was looking to wire up this directive to call a web service api whenever there was a change in the value. I saw that there is a function that I can place on the switch-change attribute, however, I noticed that this is getting called on first render (when the page loads) as opposed to when the user has interacted with the switch.

I tested this using :
//model:

$scope.model = {
available : false
}

//onChange Function

$scope.onChange = function()
{
console.log("onChange triggered...");
//I was expecting to call the web-service within this method. 
}

My element :

<input
    bs-switch
    ng-model="model.available"
    type="checkbox"
    switch-active="{{ isActive }}"
    switch-readonly="{{ isReadonly }}"
    switch-size="{{ size }}"
    switch-animate="{{ animate }}"
    switch-label="{{ label }}"
    switch-icon="{{ icon }}"
    switch-on-text="{{ onLabel }}"
    switch-off-text="{{ offLabel }}"
    switch-on-color="{{ on }}"
    switch-off-color="{{ off }}"
    switch-radio-off="{{ radioOff }}"
    switch-label-width="{{ labelWidth }}"
    switch-handle-width="{{ handleWidth }}"
    switch-inverse="{{ inverse }}"
    switch-change="onChange()"
    ng-true-value="'yep'"
    ng-false-value="'nope'">

@pradt
Copy link
Author

pradt commented Sep 9, 2016

I tried to debug this and noticed that the switchChange() is being called upon a model change. I'm wondering why this would need to call the switchChange function if a model is changed ?

          controller.$render = function () {
            initMaybe();
            var newValue = controller.$modelValue;
            if (newValue !== undefined && newValue !== null) {
              element.bootstrapSwitch('state', newValue === getTrueValue(), true);
            } else {
              element.bootstrapSwitch('indeterminate', true, true);
              controller.$setViewValue(undefined);
            }
            switchChange();
          };

@frapontillo
Copy link
Owner

What do you mean? switch-change is called every time the model changes. If you want to listen to view changes, please use ng-change.

@pradt
Copy link
Author

pradt commented Sep 11, 2016

ok I'll try that, what's switch-change for ?

@frapontillo
Copy link
Owner

It's for model changes.

@millenjo
Copy link

millenjo commented Oct 6, 2016

The problem is most likely that the ng-model value is evaluated as undefined when the template is rendered and when the controller sets available to false the directive interprets it as a model change, which it strictly speaking is even though one might argue that a ng-model change from undefined->true/false should not trigger a model change since it's almost always just an init case.

The problem can be solved by adding an ng if like this

ng-if="model.available !== undefined"

to the bs-switch input or a wrapping element, ng-show will not work since that wont stop the template from rendering the input element.

@Firzenizer
Copy link

I also ran into this and I find it unexpected behaviour. In my app an undefined settings item gets initialized and this is triggering switch-change. Using ng-change now. It works as I wanted it to work.

@frapontillo
Copy link
Owner

Please re-open if you still have this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants