1

I'd like to know whether it's possible to remove Angular expressions dynamically. I tried the following without any success:

My button

<button myDirective [disabled]="someExpression">Clippy</button>

My Directive

@Directive({
   selector: '[myDirective]'
})
export class MyDirective {
   constructor(private element: ElementRef) {}

   ngOnInit() {
      this.element.nativeElement.removeAttribute('disabled');
   }
}

The problem

Initially the button won't be disabled, but once someExpression re-evaluates it'll add the disabled attribute back to the element.

Just for clarification, I want to remove an Angular expression dynamically. In the above example it's [disabled]. But this can be any binding in the future. I want my directive to overrule the existing binding.

3
  • you can achieve this by setting @Input property to the custom directive and achieve this Commented Dec 6, 2017 at 13:00
  • You don't need a Directive to remove [disable] (not for removeAttribute) "someExpression" can be a boolean variable in your component Commented Dec 6, 2017 at 13:05
  • @Eliseo no, disabled is just an example. I want my directive to overrule the, in this case, disabled attribute Commented Dec 6, 2017 at 13:10

5 Answers 5

3

As workaround you can try this:

@Directive({
  selector: '[myDirective]'
})
export class MyDirective {
  @Input() disabled;

  constructor(private element: ElementRef) { }

  ngOnChanges() {
    if (this.disabled) {
      this.element.nativeElement.removeAttribute('disabled');
    }
  }
}

Stackblitz Example

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

Comments

1

Use @HostBinding, e.g.

import { Directive, HostBinding } from '@angular/core';

@Directive({
  selector: '[statusDirective]',
})
export class StatusDirective {
  @HostBinding('disabled') disable = true;
  constructor() {}
}

//Your input never enabled
//<input type="text" name="id" [disabled]="false" statusDirective  >

1 Comment

This works untill the expression changes, I think I have to use ngOnChanges as posted by @yurzui
0
<button myDirective [disabled]="hideAttr">Clippy - {{hideAttr}}</button>

the disabled attribute is not removed when hideAttr is false

https://plnkr.co/edit/h86IUsny6MiLfRI9tsPx

3 Comments

I know, that's not what I want. I want to remove the entire binding
@Dieterg ok Thanks
you can use @hostBinding
0

You probably need two copies of the button but only one of them displayed at a time

<ng-container *ngIf="showButtonWithDisabledExpr">
        <button [disabled]="someExpression">Clippy</button>
</ng-container>

<ng-container *ngIf="!showButtonWithDisabledExpr">
        <button>Clippy</button>
</ng-container>

Comments

0

In newer Angular versions you need to make sure the expression evaluates to null or undefined:

When the expression resolves to null or undefined, Angular removes the attribute altogether.

See Attribute Binding.

So an example would be:

<button [disabled]="someExpression ? '' : null">Clippy</button>

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.