Created
May 25, 2016 19:15
-
-
Save davidsekar/ccecef096cf5e41e58fdc14dfd1f58be to your computer and use it in GitHub Desktop.
Angular directive for iCheck checkbox & radio buttons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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