Skip to content

Instantly share code, notes, and snippets.

@davidsekar
Created May 25, 2016 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidsekar/ccecef096cf5e41e58fdc14dfd1f58be to your computer and use it in GitHub Desktop.
Save davidsekar/ccecef096cf5e41e58fdc14dfd1f58be to your computer and use it in GitHub Desktop.
Angular directive for iCheck checkbox & radio buttons
angular.module("app")
.directive('icheck', function ($timeout, $parse) {
return {
require: 'ngModel',
link: function ($scope, element, $attrs, ngModel) {
return $timeout(function () {
var value;
value = $attrs['value'];
$scope.$watch($attrs['ngModel'], function (newValue) {
$(element).iCheck('update');
});
$scope.$watch($attrs['ngDisabled'], function (newValue) {
$(element).iCheck(newValue ? 'disable' : 'enable');
$(element).iCheck('update');
})
return $(element).iCheck({
checkboxClass: 'icheckbox_square-blue',
radioClass: 'iradio_square-blue'
}).on('ifChanged', function (event) {
if ($(element).attr('type') === 'checkbox' && $attrs['ngModel']) {
$scope.$apply(function () {
return ngModel.$setViewValue(event.target.checked);
})
}
}).on('ifClicked', function (event) {
if ($(element).attr('type') === 'radio' && $attrs['ngModel']) {
return $scope.$apply(function () {
//set up for radio buttons to be de-selectable
if (ngModel.$viewValue != value)
return ngModel.$setViewValue(value);
else
ngModel.$setViewValue(null);
ngModel.$render();
return
});
}
});
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment