Skip to content

Instantly share code, notes, and snippets.

@jrwebdev
Last active May 17, 2016 06:54
Show Gist options
  • Save jrwebdev/ff069bc1a516b6b3fbe0179dbf454070 to your computer and use it in GitHub Desktop.
Save jrwebdev/ff069bc1a516b6b3fbe0179dbf454070 to your computer and use it in GitHub Desktop.
Angular Toggle Directive
const module = angular.module('toggle', []);
module.directive('toggle', function () {
return {
controllerAs: 'toggle',
bindToController: {
value: '=',
trueLabel: '@',
falseLabel: '@',
onToggle: '&'
},
controller: function () {
this.toggle = () => {
this.value = !this.value;
this.onToggle({value: this.value});
};
},
template: `
<div ng-click="toggle.toggle()">
<span ng-class="{'selected': toggle.value}">
{{toggle.trueLabel || 'Yes'}}
</span>
<span ng-class="{'selected': !toggle.value}">
{{toggle.falseLabel || 'No'}}
</span>
</div>
`
}
});
<toggle value="value"
true-label="{{trueLabel}}"
false-label="{{falseLabel}}"
on-toggle="onToggle(value)">
</toggle>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment