切换按钮 angularjs 指令不改变状态

toggle button angularjs directive doesn't change state

当我将它注入小部件时,我的指令切换按钮没有采用 ng-change 属性。

指令是:

// @ngInject
exports.abToggleButton = function($parse) {

    function templateFn() {
        return '<div class="ab-component-toggle-button">' +
            '<label>' +
            '<input type="checkbox" ng-model="ngModel" ng-change="scope.changed">' +
            '<strong></strong>' +
            '</label>' +
            '</div>';
    }

    function linkFn(scope, element, attrs) {
        scope.changed = function () {
            scope.$emit('ab-component-toggle-button.changed', { id: scope.id, enabled: scope.ngModel});
        };
    }

    return {
        restrict: 'E',
        scope: {
            id: '=',
            ngModel: '=',
            change: '&'
        },
        link: linkFn,
        template: templateFn
    };
};

在 html 标签中是:

<ab-toggle-button class="col-lg-3" ng-model="limit.selected" ng-change="ctrl.checkChangesState"></ab-toggle-button>

在我的控制器函数中是:

ctrl.checkChangesState = function () {
        console.log('test')
    };

所以当我改变切换按钮的状态时。 ng-change 不起作用。什么可能是一个问题?谢谢大家。

只是猜测,但也许您的指令输入:

change: '&',

应该变成

ng-change : '='

因为您使用的指令不是使用 change=,而是 ng-change ?