javascript - 如何在 Angular 中禁用 img?

标签 javascript jquery angularjs

我有一个 img,它有一个处理点击事件的属性指令。我希望能够禁用启用处理程序。我该怎么做?

JS

.directive('choose', function() {
    return {
        restrict: 'A',
        link: function(scope, el, attrs) {
            el.on('click', function() {
                scope.$apply(function() {
                    // stuff
                    // disable el
                });
            });
        }
    };
})
.controller('QuizCtrl', function ($scope, Quiz, $routeParams) {
    this.next = function() {
        // enable ALL els
    };
});

HTML

<img choose />
<button ng-click="q.next()">Next</button>

最佳答案

您也可以使用 css 来做到这一点:

JS

.directive('choose', function() {
    return {
        restrict: 'A',
        link: function(scope, el, attrs) {
            el.on('click', function() {
                scope.$apply(function() {});
            });
            scope.enable = function(){
                el.css({pointerEvents: ''});
            };
            scope.disable = function(){
                el.css({pointerEvents: 'none'});
            };
        }
    };
})

关于javascript - 如何在 Angular 中禁用 img?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26856512/

相关文章:

javascript - 在数据表中使用 PHP 和 JavaScript 显示 MySQL 表

javascript - Mozilla 扩展的内容安全策略

javascript - react 导航 : styling TabNavigator text

javascript - 如何从一组可渲染组件构建 JSX 组件树?

javascript - 更改模糊的CSS

AngularJs : Passing parameter to ng-click in directive

Javascript/jQuery 元素在页面重新加载之前不起作用

jquery - 如何在 jQuery 方法中包含 javascript 变量?

javascript - 优化循环 ui 更新

javascript - Angular ng-repeat,元素外的内容