Skip to content

Custom method + on/off custom values #38

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ Add optional on/off text
</form>
```

Add custom values for `ON` / `OFF` (if your model is not based on true/false values)
```html
<form>
<switch id="enabled" name="enabled" ng-model="enabled" on-value="ACTIVE" off-value="INACTIVE" class="green"></switch>
<br>{{ enabled }}
</form>
```

Add a custom method to `on-click` event
```html
<form>
<switch id="enabled" name="enabled" ng-model="enabled" on-click-change="myMethod(with, args)" class="green"></switch>
<br>{{ enabled }}
</form>
```

Disabled state
```html
<form>
Expand Down
20 changes: 18 additions & 2 deletions angular-ui-switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@ angular.module('uiSwitch', [])
, transclude: true
, template: function(element, attrs) {
var html = '';
var extraMethod = '';

if (attrs.ngOnClick) {
extraMethod += '; ' + attrs.ngOnClick;
}
if (attrs.ngChange) {
extraMethod += '; ' + attrs.ngChange + '()';
}

html += '<span';
html += ' class="switch' + (attrs.class ? ' ' + attrs.class : '') + '"';
html += attrs.ngModel ? ' ng-click="' + attrs.disabled + ' ? ' + attrs.ngModel + ' : ' + attrs.ngModel + '=!' + attrs.ngModel + (attrs.ngChange ? '; ' + attrs.ngChange + '()"' : '"') : '';
html += ' ng-class="{ checked:' + attrs.ngModel + ', disabled:' + attrs.disabled + ' }"';

if (attrs.onValue && attrs.offValue) {
html += attrs.ngModel ? ' ng-click="' + attrs.disabled + ' ? ' + attrs.ngModel + ' : ' + attrs.ngModel + '=(' + attrs.ngModel + '=== \'' + (attrs.onValue || true) + '\' ? \'' + (attrs.offValue || false) + '\':\'' + (attrs.onValue || true) + '\')' + extraMethod + '"' : '';
html += ' ng-class="{ checked:' + attrs.ngModel + '===\'' + (attrs.onValue || true) + '\'?' + 'true' + ':' + 'false' + ', disabled:' + attrs.disabled + ' }"';
} else {
html += attrs.ngModel ? ' ng-click="' + attrs.disabled + ' ? ' + attrs.ngModel + ' : ' + attrs.ngModel + '=!' + attrs.ngModel + extraMethod +'"' : '';
html += ' ng-class="{ checked:' + attrs.ngModel + ', disabled:' + attrs.disabled + ' }"';
}

html += '>';
html += '<small></small>';
html += '<input type="checkbox"';
Expand Down
2 changes: 1 addition & 1 deletion angular-ui-switch.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.