36

Is there a way to disable an entire div tag in angularjs. I have a div tag that contains inside it several form fields that I want to disable with just one condition. Is this possible? Below is my code.I tried using but it did not work. Any suggestions would be helpful. Thanks, please see code below. I have the main div tag that contains the form. I want to disable the form based on a condition, so user cannot enter the id and name text fields.

<div>
<form name="form" role="form" novalidate
class="ng-scope ng-invalid ng-invalid-required ng-dirty ng-valid-minlength"
ng-submit="createStudy()">



<div class="form-group">
    <label>ID</label> <input type="text" class="form-control"
        name="id" ng-model="study.id">
</div>

<div class="form-group">
    <label>Name</label> <input type="text" class="form-control"
        name="name" ng-model="study.name" ng-minlength=1
        ng-maxlength=50 ng-required="true">
</div>



<div class="modal-footer">
   <a href="#/studies"><button type="button" class="btn btn-default"
        data-dismiss="modal" ng-click="clear()">
        <span class="glyphicon glyphicon-ban-circle"></span> Cancel
    </button></a>
    <button type="submit" ng-disabled="form.$invalid"
        class="btn btn-primary">
        <span class="glyphicon glyphicon-save"></span> Save
    </button>
</div>

</form>
</div>

9 Answers 9

60

You can use 'fieldset' instead of 'div' and it will disable all form elements inside it if you add ng-disabled to it. Please find below :

<fieldset ng-disabled="true"> 
<label>ID</label> <input type="text" class="form-control"
        name="id" ng-model="study.id">
<label>Name</label> <input type="text" class="form-control"
        name="name" ng-model="study.name" ng-minlength=1
        ng-maxlength=50 ng-required="true">
</fieldset>
Sign up to request clarification or add additional context in comments.

2 Comments

This sets the 'disabled' attribute on the fieldset, which doesn't actually disable its form elements in IE 9 and below github.com/twbs/bootstrap/issues/8701
Not working as per requirement, still able to access all elements of the fieldset.
18

You can edit the css/less to disable all elements inside the disabled div.

For example:

set the .disabled-class in html:

<div ng-disabled="disableExpression" 
     ng-class="{'disabled': disableExpression}"
>
  <!-- content -->
</div>

style your disabled div:

.disabled{
  opacity: 0.5;
  pointer-events: none;

  > * {
    opacity: 0.5;
    pointer-events: none;
  }
}

1 Comment

Still accessible using a keyboard
7

you can easily create your own directive for this, this will disable all the inputs inside the element (form in your case);

yourModule.directive('disableForm',function(){

    return{
       scope:{
         disableForm:'=' 
       }
       link:function(scope, element ){
         if(scope.disableForm){
          angular.element('input',element).attr('disabled','disabled') ;
         }
       }
     }
    });

and in HTML,

<form name="form" role="form" novalidate disable-form="condition" ...

Comments

7

in the main <div> you write:

<div ng-class="{'my-disable':condition}">

in the css file:

.my-disable{
    pointer-events:none;
}

2 Comments

This worked perfectly! However, it's a relatively new CSS attribute that isn't supported in older browsers.
This will not work if user navigates or move across the field using tab and type something.
2

This directive has worked nicely for me: https://github.com/PLEEROCK/angular-disable-all

Comments

1

Div does not have concept of disable. Input have a concept of readonly. You can use the ng-readonly attribute with binding to the variable that determines whether the form is read only

<input type="text" class="form-control" name="id" ng-model="study.id" ng-readonly="readMode">

1 Comment

It should, check documentation for ng-readonly code.angularjs.org/1.2.15/docs/api/ng/directive/ngReadonly and there is a sample too.
1

there is way which @Pavi mention to it, using fieldset :

<fieldset ng-disabled="true"> 
</fieldset >

but in my case that doesn't work so I changed it like this:

<fieldset  [attr.disabled]="true">
</fieldset >

worked perfectly.

Comments

0

Simple, create a variable for example "disableAll" and use ng-disabled="disableAll" in places where you want to control enable/disable. In below example, I modified your original code and you can enable and disable it by changing below line:

<div ng-init="disableAll=true" >

here is the code:

<div ng-controller='myCtrl'>

<div ng-init="disableAll=true" >
<form name="form" role="form" novalidate
class="ng-scope ng-invalid ng-invalid-required ng-dirty ng-valid-minlength"
ng-submit="createStudy()" >



<div class="form-group" >
    <label>ID</label> <input type="text" class="form-control"
        name="id" ng-model="study.id" ng-disabled="disableAll">
</div>

<div class="form-group" >
    <label>Name</label> <input type="text" class="form-control"
        name="name" ng-model="study.name" ng-minlength=1
        ng-maxlength=50 ng-required="true"  ng-disabled="disableAll">
</div>



<div class="modal-footer">
   <a href="#/studies"><button type="button" class="btn btn-default"
        data-dismiss="modal" ng-click="clear()"   ng-disabled="disableAll">
        <span class="glyphicon glyphicon-ban-circle"></span> Cancel
    </button></a>

    <button type="submit"   ng-disabled="disableAll"
        class="btn btn-primary">
        <span class="glyphicon glyphicon-save"></span> Save
    </button>
</div>

</form>
</div>
</div>

Comments

0

use [disabled] directive on <fieldset> instead of div

<fieldset [disabled]="isToDisable()"></fieldset>

Note:
to disabled included elements try to use also <fieldset> elements inside

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.