1

I have created a directive that disable all of the selected child elements like this :

app.directive('noeDisable', function () {
        var linkFunction = function (scope, element, attributes) {
            scope.text = attributes["=noeDisable"];
            if (scope.text == 'true') {
                $(element).find('input,button,a').attr("disabled", true);
            }
        };
        return {
            link: linkFunction

        };
    });

and it work well for this example: <div noe-disable="true"> ... </div>. but the problem is some of the child elements load later, for example after ajax call or when I have another angularjs directive inside parent element that add some child elements to its parent, so they wont be disabled !!! How can I deal with this problem ?

10
  • 1
    Nice question. Are you targeting old browsers? There are serveral solutions to this, but the nicest ones are ever-green only. Commented Oct 6, 2014 at 5:36
  • @SanderElias I use modern browsers like google chrome version 37 Commented Oct 6, 2014 at 5:38
  • Good! you can use an mutationObsever then. One question left, The thing you add to your directive, are those direct children to this directive, or is there a (number of) layers in between? Commented Oct 6, 2014 at 5:42
  • Add $watch on the variable that gets updated after the ajax call and execute the disable routine. Commented Oct 6, 2014 at 5:45
  • 1
    you can use fieldset to disable all child fields in that fieldset Commented Apr 13, 2015 at 9:38

2 Answers 2

7

you can use fieldset.

Wrap all your fields in fieldset and use ng-disabled like:

<fieldset ng-disabled="shouldDisabled"> 
         ... inputs ...
</fieldset>

It will automatically disable all inputs inside the fieldset.

Sign up to request clarification or add additional context in comments.

5 Comments

that's an elegant solution :)
Doesn't work on IE, works on Edge but no version of IE
However, while it works for angular material inputs it does not work for angular material buttons.
<fieldset [disabed]="myConditoin"> ... worked for me
@Yomi1984 That's because you're using Angular 2+
2

Had a spare (couple off) minutes!

Here is the plunk showcasing the mutationObserver.

the crux is in this line:

observer.observe(element[0], config);

witch subscribe to all the dom updates of the element. I ditched the dependency on jQuery, while I was at it ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.