【问题标题】:toggle button angularjs directive doesn't change state切换按钮 angularjs 指令不会改变状态
【发布时间】:2018-10-25 08:59:10
【问题描述】:

当我将指令切换按钮注入小部件时,它不采用 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 不起作用。可能有什么问题?谢谢大家。

【问题讨论】:

    标签: javascript angularjs angularjs-directive


    【解决方案1】:

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

    更改:'&',

    应该变成

    ng-change : '='

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

    【讨论】:

    • 我将 ng-change 更改为更改:'='。它开始起作用了。谢谢你,卡尔!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 2014-02-03
    相关资源
    最近更新 更多