I\'m trying to integrate JQuery.iCheck (plugin for checkbox&Radio buttons
styling).
I followed few suggestions here that said that the way to integrate a jQ
With the code from XaReSx answer I had one issue : When updating the model from the scope the radio doesn't changed. I had to add a timeout in the model watch :
$scope.$watch($attrs['ngModel'], function (newValue) {
$timeout(function () {
$(element).iCheck('update');
})
})
Here is a better sololution by hackthis02
.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
});
}
});
});
}
};
});
Already solved on this link: https://github.com/fronteed/iCheck/issues/62 by wajatimur
webApp.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');
})
return $(element).iCheck({
checkboxClass: 'icheckbox_flat-aero',
radioClass: 'iradio_flat-aero'
}).on('ifChanged', function(event) {
if ($(element).attr('type') === 'checkbox' && $attrs['ngModel']) {
$scope.$apply(function() {
return ngModel.$setViewValue(event.target.checked);
});
}
if ($(element).attr('type') === 'radio' && $attrs['ngModel']) {
return $scope.$apply(function() {
return ngModel.$setViewValue(value);
});
}
});
});
}
};
});