How can I create a directive that adds ng-class and ng-disabled on the target element based on a condition?

I have the following code:

app.directive "ngDisableOnVar", ($compile) ->
  restrict: "A"
  terminal: true
  priority: 1000
  scope: {}
  compile: compile = (element, attrs) ->
    cattr = attrs["ngDisableOnVar"]
    element.attr("ng-class", "{'disabled': !#{cattr}}")
    element.attr("ng-disabled", "!#{cattr}")
    pre: preLink = (scope, iElement, iAttrs, controller) ->

    post: postLink = (scope, iElement, iAttrs, controller) ->

I tried to base the code on the answer given here. Basically, I'd like to have the following:

<input ngDisableOnVar="someScopeVariable>

And have it replaced with the following:

<input ng-class="{'disabled': !someScopeVariable}" ng-disabled="!someScopeVariable">

Something is wrong, cause even though I have them applied to my element, they're always disabled, even though the scope variable evaluates to true. What am I doing wrong?

EDIT: I created a plunker, where the first 2 buttons are created with ng-class and ng-disabled, and the other 2 buttons, should have the same things applied on them through the use of the directive.

Here is the plunker version with shared scope:

And here's the one without the shared scope:

The problem is, the one without the shared scope does not update. How can I make them update, and have the conditions depend on the variables passed as arguments?

EDIT #2: I'm starting to believe that the scope sharing is the correct way these 2 buttons should act, short of creating a new directive that encapsulates both buttons within it. Not 100% sure though.


I would go with your EDIT #2 because they are related. If we create them as separate elements, we need to somehow pass the related element to each one => When we click on 1 button, we update itself and also the related element.

Here I modified your first approach to make it work:

In this example, I have to pass the related element to each directive so that when we click we can update itself and the related element:


I did some modifications in the directive:

scope: {
      reactOn: "=", //use property binding instead of function binding
link: function(scope, element, attrs) { 
      scope.toggle = function(){
        scope.reactOn = !scope.reactOn;//toggle current element
        var relatedScope = $("#"+scope.relatedElement).scope();//get related element's scope and toggle it
        relatedScope.reactOn = !relatedScope.reactOn;
      //var cattr = attrs.ngDisableReactOn;
      element.attr("ng-class", "{'disabled': !reactOn}"); //Use reactOn instead as this is the property of current scope
      element.attr("ng-disabled", "!reactOn");
      element.attr("ng-click", "toggle()"); 

We don't need to make things complex. Just create a normal directive to wrap 2 buttons.

myApp.directive("ngDisableReactOn", function($compile) {
  return {
    restrict: "A",
    scope: {
      can_go_back: "@"
    link: function(scope, element, attrs) { 
      scope.goBack = function(){
          scope.can_go_back = false;

      scope.goFwd = function(){
          scope.can_go_back = true;


<input type="button" value="go back" ng-click="goBack()"  ng-class="{'disabled': !can_go_back}" ng-disabled="!can_go_back">
<input type="button" value="go fwd"  ng-click="goFwd()"   ng-class="{'disabled': can_go_back}" ng-disabled="can_go_back">


Another solution is to create a parent directive as a container. This is the solution I like the most. With this approach, you can freely change the inner content of the directive like adding more buttons, more text,....(DON'T NEED TO HARDCODE THE TEMPLATE) The parent directive works as a manager to ensure there is only 1 active child at a time:

myApp.directive("ngDisableReactOnContainer", function() { //Container directive to manage all child directives
  return {
    restrict: 'EA',
    replace: true,
    transclude: true,//Use transclusion to move inner content to the template
    template: '<div ng-transclude></div>',
    controller: function() {
      var children = []; 
      this.selectChild = function(activeChild) { //ensure that only 1 child is active at a time = true;
        angular.forEach(children, function(child) {
          if (child != activeChild) {
   = false;

      this.addChild = function(child) {

myApp.directive("ngDisableReactOn", function($compile) {
  return {
    restrict: "A",


    require: '^?ngDisableReactOnContainer',
    link: function(scope, element, attrs, controller) { = === 'true';

      controller.addChild(scope);//register itself with the container = function(){//When this element is clicked, inform the container to toggle all children accordingly.

      //Add ng-class and ng-disabled based on your requirement.
      element.attr("ng-class", "{'disabled': active}"); //Use active instead as this is the property of current scope
      element.attr("ng-disabled", "active");
      element.attr("ng-click", "select()"); 

Using these directives would be simple:

<div ng-disable-react-on-container>
    <input ng-disable-react-on type="button" value="button 1" active="true" >
    <input ng-disable-react-on type="button" value="button 2" >
    <input ng-disable-react-on type="button" value="button 3" >


Here is a very ugly way to demonstrate how to compile the template during the link function. It is ugly because I didn't address any binding on the scope variable. You might want to isolate the scope or setup two-way binding but this should give you the gist of how to access the scope for compiling purposes.

app.directive('foo', function($compile) {
  return function(scope, elem, attrs) {
    var html;
    if (scope.testVar)
      html = '<input ng-class="{\'disabled\': !someScopeVariable}" ng-disabled="!someScopeVariable" />';
      html = '<input />';
    var htmlEl = angular.element(html),
      compiledEl = $compile(htmlEl)(scope);

You can get same effect with another similar approach. Plunk here

Instead of $compile in link function you can use template in your directive and ng-disabled with a variable in scope that is bind to parent scope variable via isolated scope.

Have you tried to remove the ! before the var name?

<input ng-class="{'disabled': someScopeVariable}" ng-disabled="someScopeVariable">

Need Your Help

Visual studio sql Server insert form html sql-server database

I have to do this thing for work that's really just a formality. Normally I'm a linux/UNIX guy but they're making me do this. I need to have an asp webpage that displays data from two different dat...

Regex for parsing queryString

java regex

Hi am looking for an Regular Expression which parses QueryString.Am using the below regex: