admin管理员组文章数量:1323714
I created one directive for checkbox which makes an image layer, but problem is that it's not firing ng change event if even input box checked value is changed
Directive
function icheck($timeout,$parse) {
return {
restrict: 'A',
link: function ($scope, element, $attrs, $watch) {
var value = $attrs['value'],
ngModelGetter = $parse($attrs['ngModel']);
return $timeout(function () {
$scope.$watch($attrs.ngModel, function (newValue) {
$(element).iCheck('update');
});
$(element).iCheck({
checkboxClass: 'icheckbox_square-green',
radioClass: 'iradio_square-green'
}).on('ifChanged', function (event) {
var elemType = $(element).attr('type');
if (elemType === 'checkbox' && $attrs.ngModel) {
$scope.$apply(function () {
console.log(event.target.checked)
return ngModelGetter.assign($scope, event.target.checked);
});
}
else if (elemType === 'radio' && $attrs.ngModel) {
return $scope.$apply(function () {
return ngModelGetter.assign($scope, value);
});
}
});
});
}
};
}
HTML BELOW
<input type="checkbox" class="i-checks" data-icheck ng-change="alert('changed!')" ng-model="chekal" id="chkSelectAll">
Any idea how I can trigger ng change event on even click event will be fine.
I created one directive for checkbox which makes an image layer, but problem is that it's not firing ng change event if even input box checked value is changed
Directive
function icheck($timeout,$parse) {
return {
restrict: 'A',
link: function ($scope, element, $attrs, $watch) {
var value = $attrs['value'],
ngModelGetter = $parse($attrs['ngModel']);
return $timeout(function () {
$scope.$watch($attrs.ngModel, function (newValue) {
$(element).iCheck('update');
});
$(element).iCheck({
checkboxClass: 'icheckbox_square-green',
radioClass: 'iradio_square-green'
}).on('ifChanged', function (event) {
var elemType = $(element).attr('type');
if (elemType === 'checkbox' && $attrs.ngModel) {
$scope.$apply(function () {
console.log(event.target.checked)
return ngModelGetter.assign($scope, event.target.checked);
});
}
else if (elemType === 'radio' && $attrs.ngModel) {
return $scope.$apply(function () {
return ngModelGetter.assign($scope, value);
});
}
});
});
}
};
}
HTML BELOW
<input type="checkbox" class="i-checks" data-icheck ng-change="alert('changed!')" ng-model="chekal" id="chkSelectAll">
Any idea how I can trigger ng change event on even click event will be fine.
Share Improve this question asked Aug 5, 2015 at 6:02 nirmalnirmal 2,1801 gold badge21 silver badges29 bronze badges4 Answers
Reset to default 3Documentation of ngChange:
Evaluate the given expression when the user changes the input
If you want to watch the model, use $scope.watch
$scope.$watch('chekal', function(newvalue,oldvalue) {
if (newvalue !== oldvalue){
//write code here
}});
What i did was passing funciton value in attribute
<input type="checkbox" class="i-checks" data-icheck data-function="selectAll" ng-model="chekal" id="chkSelectAll">
directive
if (elemType === 'checkbox' && $attrs.ngModel) {
$scope.$apply(function () {
console.log(event.target.checked)
var f = $(event.target).data('function');
console.log(f);
if (f !== "undefined" || f !== "") {
eval('$scope.' + f + '(event)');
}
return ngModelGetter.assign($scope, event.target.checked);
});
}
so I called the function with that eval thing. This answer is just for reference so it can help others as well.
I don't wanted to add adhoc of watches so did it without increasing watches and solved question with more performance way.
Change your icheck
function to directive
as below:
yourapp.directive('icheck', function($timeout,$parse) {
return {
restrict: 'A',
link: function ($scope, element, $attrs, $watch) {
var value = $attrs['value'],
ngModelGetter = $parse($attrs['ngModel']);
return $timeout(function () {
$scope.$watch($attrs.ngModel, function (newValue) {
$(element).iCheck('update');
});
$(element).iCheck({
checkboxClass: 'icheckbox_square-green',
radioClass: 'iradio_square-green'
}).on('ifChanged', function (event) {
var elemType = $(element).attr('type');
if (elemType === 'checkbox' && $attrs.ngModel) {
$scope.$apply(function () {
console.log(event.target.checked)
return ngModelGetter.assign($scope, event.target.checked);
});
}
else if (elemType === 'radio' && $attrs.ngModel) {
return $scope.$apply(function () {
return ngModelGetter.assign($scope, value);
});
}
});
});
}
};
});
And add data-i-check
as an attribute to input tag instead of data-icheck
.
<input type="checkbox" class="i-checks" data-i-check ng-change="alert('changed!')" ng-model="chekal" id="chkSelectAll">
$scope.$watch('chekal', function(newvalue,oldvalue) {
if (newvalue && newvalue !== oldvalue){
// here you go
}
});
本文标签: angularjsng change event not firing on checkbox if it39s set by javascriptStack Overflow
版权声明:本文标题:angularjs - ng change event not firing on checkbox if it's set by javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742124792a2421888.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论