5

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.

4 Answers 4

3

Documentation 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
          }});
Sign up to request clarification or add additional context in comments.

5 Comments

solved 90% but now issue is that when I refresh page then that function executes first like I alerted some thing now it's firing first on page load
@cverb This shouldn't be necessary, since ng-change should fire if used correctly.
@thwiegan As he is saying in his title, the event doesn't fire when it's set via javascript, which is just normal behaviour (see the Angular documentation). @nirmal Put if (newvalue !== oldvalue) check in your watch function.
@thwiegan ng change event is not firing don't know reason may be as I am changing through directive. But now thing is that it triggers event on page load first time
@livepo it worked but I have used mine solution, that's why I have not accepted the answer not even mine but upvote for sure thanks cverb
2

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.

2 Comments

If this worked, then you ought to accept it as the answer
Np. It is always ok to accept your own answer if there is no other, better answer. That way, people reading the question in future know what the solution is (you probably forgot because you can't accept your own answer until two days after you post the question).
0

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">

1 Comment

it's already directive, please read the question and still not triggering change event
0
$scope.$watch('chekal', function(newvalue,oldvalue) {
  if (newvalue && newvalue !== oldvalue){
      // here you go
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.