Why does ng-change always pass 'true' from model

157 views Asked by At

When using the ng-change directive and passing model information to a function, ng-change always passess: true; regardles if the switch is on/off. I would like to receive both states of true and false depending on on/off state.

I am using a custom switch directive based on switchery and angular-ui-switch

<switch 
    ng-model="app.layout.isNavbarFixed"
    ng-change="uiSettingTypeChange('isNavbarFixed',
         {{app.layout.isNavbarFixed}})"
    class="green">
 </switch>

and

$scope.uiSettingTypeChange = function(name, setting) {
  console.log(name +' | ' + setting);
};

console.log always outputs:

isNavbarFixed | true
1

There are 1 answers

1
dnc253 On BEST ANSWER

The value of the ng-change attribute is already an angular expression so there is no need for the {{}}. What I guess is happening, is that angular sees the {{}} and thus evaluates it. So, at that point the ng-change effective looks like this:

ng-change="uiSettingTypeChange('isNavbarFixed',true)"

Then, when the change event is firing, it's just passing that true in every time. What you want should look more like this:

ng-change="uiSettingTypeChange('isNavbarFixed',app.layout.isNavbarFixed)"