AngularJS禁用指令

use*_*415 2 javascript angularjs

我正在使用AngularJS模块的editable-text指令xeditable.有没有办法禁用整个页面的指令?

我考虑过使用替换editable-text {{variable}},在哪里variable="editable-text"启用和variable="somethingElse"禁用.但是,这会在html中产生无意义的属性.

Kla*_*r_1 8

可以使用另一个指令删除指令.为此,新指令应该比被删除的指令具有更高的优先级,然后在编译状态下,您搜索具有required指令的元素,并整体删除tag/class/element.这是一个非常简单的实现:

.directive("disableDirective", function () {
    function compile (el, attr) {
        var hasDirective = el[0].querySelectorAll("["+attr.disableDirective+"]");

        [].forEach.call(hasDirective, function (el) {
            el.removeAttribute(attr.disableDirective);
        });
    }

    return {
        priority: 100000,
        compile: compile
    };
})
Run Code Online (Sandbox Code Playgroud)

在下面的HTML DIV将是可见的,这要归功于我们的指令:

<body disable-directive="ng-hide">
  <div ng-hide="true">Hidden</div>
</body>
Run Code Online (Sandbox Code Playgroud)

您必须disable-directive="editable-text"为页面设置.

JSBin.