将iCheck插件用angularjs的方式封装成组件
发布时间:2020-12-17 08:30:18 所属栏目:安全 来源:网络整理
导读:最近做的angularjs工程项目想把多选和单选框变得更美观一点,就在项目里引用了iCheck插件。 但是使用了icheck插件后,会生成一个美化过的div覆盖在原来的checkbox或者radio之上,而原来的checkbox或者radio会被隐藏。 故而,当点击多选框时,不会直接触发事
最近做的angularjs工程项目想把多选和单选框变得更美观一点,就在项目里引用了iCheck插件。 /* * angular directive ng-icheck * * @require jquery,icheck * @example <input type="radio" ng-model="radioCheck" ng-icheck /> * <input type="checkbox" ng-model="checkboxCheck" ng-icheck /> */
myDirective.directive('ngIcheck',['$timeout',function($timeout) {
return {
require: 'ngModel',link: function($scope,element,$attrs,ngModel) {
return $timeout(function() {
$scope.$watch($attrs['ngModel'],function (newValue) {
$(element).iCheck('update');
});
return $(element).iCheck({
checkboxClass: 'icheckbox_minimal-blue',radioClass: 'iradio_minimal-blue'
}).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($attrs['value']);
});
}
});
});
}
};
}]);
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |