4

I wanted to disable a div section using ng-disabled, but it dint work. I've also tried using fieldset in place of div. ng-disabled seems not working.

Below is the view part:

<div ng-disabled="model.disableDate">
    <div>Date</div>
    <div ion-datetime-picker ng-model="model.timeEntryDate" ng-change="onTimeEntryDateChange()" date="true" time="false" monday-first="true" am-pm="true">{{model.timeEntryDate|| "Select" | date: "dd/MM/yyyy"}} <i class="icon ion-ios-calendar-outline"></i> </div>
</div>

This is the controller part:

if ($scope.model.pageState === condition) {
    $scope.model.disableDate = true;
}

Any way this calender is being popped even on the disabling condition.

3
  • 1
    You can't disable <div/>! I don't know what's there in ionic though Commented Oct 6, 2017 at 5:52
  • 2
    You can use css. Disable pointer-events. Only problem is this is new CSS, so won't work on old browsers. Commented Oct 6, 2017 at 5:58
  • I've also tried using fieldset in place of div. -- enclosing a div within the fieldset and set ng-disabled to fieldset should work. Commented Oct 6, 2017 at 6:05

3 Answers 3

15

You can't disable DIV. Disable work with button and input types only. You can try with css. Use ng-class.

<div ng-class="{ 'div-disabled': model.disableDate}"> // give condition here as per your scenario

.div-disabled
{
  pointer-events: none;
  opacity: 0.5;
  background: #CCC;
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can create an attribute directive to disable any div. See below:

  var app = angular.module("myApp", []);
        app.directive("disableSection", function() {
            return {
                restrict : "A",
                 link:function(scope,element,attr,ctrl){                   
                        element.css({
                          cursor :"not-allowed",
                          opacity: "0.5",
                          background: "#CCC"
                        });
                    }
            };
        });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
  <div disable-section>This is disabled div</div>
</body>

Comments

0

You can use pointer-events: none for disabling the div:-

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.obj = {};
  $scope.obj.disabled = false;
  
  $scope.clickMe = function(){
  alert('div is enabled');
  }
  
});
.disabled {
    pointer-events: none;
}
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
  
  <div ng-class="(obj.disabled)?'disabled':''" style="width:100px;height:100px;border:1px solid black">
  <a style="cursor:pointer" ng-click="clickMe()">Click Me</a>
  </div>
  <button ng-click="obj.disabled = !obj.disabled">{{(obj.disabled)?'Enable':'Disable'}}</button>
</div>

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.