Skip to content

Commit

Permalink
Merge pull request #15 from jfrumar/master
Browse files Browse the repository at this point in the history
Respond to ngDisabled directive on the ui-switch element.
  • Loading branch information
aramonc committed Sep 26, 2014
2 parents 746de3e + af7ede1 commit 08d5717
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/ng-switchery.js
Expand Up @@ -24,8 +24,26 @@ angular.module('NgSwitchery', [])
options = $parse(attrs.uiSwitch)(scope);
}
catch (e) {}
$timeout(function() {
var switcher = new $window.Switchery(elem[0], options);
var switcher;
var previousDisabledValue;
// Watch for attribute changes to recreate the switch if the 'disabled' attribute changes
attrs.$observe('disabled', function(value) {
if (value == undefined || value == previousDisabledValue) {
return;
} else {
previousDisabledValue = value;
}
initializeSwitch();
});

function initializeSwitch() {
$timeout(function() {
// Remove any old switcher
if (switcher) {
angular.element(switcher.switcher).remove();
}
// (re)create switcher to reflect latest state of the checkbox element
switcher = new $window.Switchery(elem[0], options);
var element = switcher.element;
element.checked = scope.initValue;
switcher.setPosition(false);
Expand All @@ -34,8 +52,11 @@ angular.module('NgSwitchery', [])
ngModel.$setViewValue(element.checked);
})
})
}, 0);
}
}, 0);
}
initializeSwitch();
}

return {
require: 'ngModel',
restrict: 'AE',
Expand Down

0 comments on commit 08d5717

Please sign in to comment.