切换按钮angularjs指令不会更改状态

时间:2018-10-25 08:59:11

标签: javascript angularjs angularjs-directive

我的指令切换按钮在将其注入窗口小部件时没有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不起作用。有什么问题吗?谢谢大家。

1 个答案:

答案 0 :(得分:0)

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

  

更改:'&',

应成为

  

ng-change:'='

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