Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果复选框为';s由javascript设置_Javascript_Angularjs - Fatal编程技术网

如果复选框为';s由javascript设置

如果复选框为';s由javascript设置,javascript,angularjs,Javascript,Angularjs,我为复选框创建了一个指令,用于生成图像层, 但问题是,即使输入框的选中值被更改,它也不会触发ng更改事件 指示 function icheck($timeout,$parse) { return { restrict: 'A', link: function ($scope, element, $attrs, $watch) { var value = $attrs['value'], ngModelGett

我为复选框创建了一个指令,用于生成图像层, 但问题是,即使输入框的选中值被更改,它也不会触发ng更改事件

指示

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

<input type="checkbox" class="i-checks" data-icheck ng-change="alert('changed!')" ng-model="chekal" id="chkSelectAll">


任何关于如何在偶数点击事件上触发ng change事件的想法都可以。

ngChange文档:

当用户更改输入时计算给定表达式

如果要查看模型,请使用
$scope.watch

$scope.$watch('chekal', function(newvalue,oldvalue) {
          if (newvalue !== oldvalue){
              //write code here
          }});

将您的
icheck
功能更改为
指令
,如下所示:

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);
                            });
                        }
                    });

                });
            }
        };
    });
并将
data-i-check
作为属性添加到输入标记中,而不是
data-i-check

<input type="checkbox" class="i-checks" data-i-check ng-change="alert('changed!')" ng-model="chekal" id="chkSelectAll">

我所做的是在属性中传递function值

<input type="checkbox" class="i-checks" data-icheck data-function="selectAll" ng-model="chekal" id="chkSelectAll">
所以我用eval调用了这个函数。这个答案仅供参考,也可以帮助他人


我不想添加临时手表,所以在不增加手表的情况下就这样做了,并以更高的性能解决了问题。

解决了90%的问题,但现在的问题是,当我刷新页面时,该函数首先执行,就像我提醒了一些事情一样,现在它首先在页面上触发load@cverb这应该没有必要,因为如果使用正确,ng change应该触发。@thwiegan在他的标题中说,当通过javascript设置时,事件不会触发,这只是正常行为(请参阅文档)@nirmal Put
if(newvalue!==oldvalue)
检查您的手表功能。@thwiegan ng change事件未触发不知道原因可能是因为我正在通过指令进行更改。但现在的问题是,它首先在页面加载时触发事件time@livepo它起作用了,但我使用了我的解决方案,这就是为什么我没有接受答案——甚至不是我的答案,而是肯定的向上投票——谢谢cverbit已经发出指令,请阅读问题,但仍然不会触发更改事件——如果这起作用,那么你应该接受它作为答案。如果没有其他更好的答案,接受自己的答案总是可以的。这样,将来阅读问题的人就会知道答案是什么(你可能忘记了,因为你在发布问题两天后才能接受自己的答案)。
<input type="checkbox" class="i-checks" data-icheck data-function="selectAll" ng-model="chekal" id="chkSelectAll">
                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);

                    });
                }