2

I am trying to disable button and input-field through angular directive based on the permission. I have tried few ways but it doesn't help

if (this.permission==true) {
      this.viewContainer.createEmbeddedView(this.templateRef);
        } else {
          const viewRootElement : HTMLElement = this.viewContainer.createEmbeddedView(
            this.templateRef
          ).rootNodes[0];
          viewRootElement.setAttribute('disabled', 'false');

        }

3 Answers 3

1
  1. For the Reactive Forms usage you may use directive for that
@Directive({
  selector: '[disableControl]'
})
export class DisableControlDirective implements OnChanges {
  @Input() disableControl = false;

  constructor(private ngControl: NgControl) {}
  ngOnChanges(changes: SimpleChanges): void {
    if (changes['disableControl'] && this.ngControl.control) {
      const action = this.disableControl ? 'disable' : 'enable';
      this.ngControl.control[action]();
    }
  }
}

Now you can use it on input or button

<input [formControl]="formControl" [disableControl]="disable">
<button (click)="disable = true">Disable</button>
<button (click)="disable = false">Enable</button>
  1. For the usual input you should just use [readonly] - it will be both reactive and working (see explanation why disabled is bad here)
Sign up to request clarification or add additional context in comments.

4 Comments

am getting Object is possibly 'null'.ts(2531) in this.control
I've updated the answer a little bit, it should be fixed now
and am not using forms either! its not working
Sorry, I thought you were talking about forms input. for the usual one please see stackoverflow.com/questions/42179150/…
0

For the button disabled you can use this:

<button class="btn btn-primary" type="submit" [disabled]="disablebtn" (click)="submit()">Upload</button>

In .ts file:

disablebtn:boolean=false;
submit(){
this.disablebtn=true;
}

For the input field you can use this:

<input type="text" [disabled]="true">

1 Comment

I was asking through the angular directive
0

First: you want to create a directive, which targets button and input.

@Directive({
  selector: 'input[dis], button[dis]',
})

Next: you need to know how to toggle the disable state of a button/input

this.render.setProperty(
            this.el.nativeElement,
            'disabled',
            true //false
          );

Last: in that directive, use your permission service to toggle the element state, in this case is disable state.

this.perService.permission$
      .pipe(
        tap((hasPermission) => {
          //get permission value and set the disable state
          );
        })
      )

You can find the full working code in the below link.
Working stackblitz

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.