7

I am working on an angular application. Application consists of a parent component which then contains multiple child components. Each child component takes care of a a part of the screen, like top bar, side bar, bottom bar etc etc.

I have a requirement in which I have to grey out some of the child components based on certain state in parent component, so that user is not able to perform any action on those components views.

How can this be achieved? I understand that ng-disabled is used for disabling view elements but it can't be used for div. So what better options are available?

2
  • [disabled]="condition" Commented Sep 26, 2019 at 16:18
  • I dont think that works for divs. Would give a try though. Commented Sep 26, 2019 at 16:23

2 Answers 2

11

You can add a conditional class to parent div something like

<div [ngClass]="{'disabled':yourCondition}">
<your-component></your-component>
</div>

then in css

.disabled
{
  pointer-events: none;
  /* for "disabled" effect */
  opacity: 0.5;
  background: #CCC;
}

Demo

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

1 Comment

We can still access the field by tabbing with the keyboard, and modify the value (see the stackblitz).
9

You can include the child component in a fieldset container, and set the disabled property of that container with property binding:

<fieldset [disabled]="!childEnabled">
  <my-component></my-component>
</fieldset>

The disabled flag will apply to all the inputs and buttons included in the container. It will also prevent the user from accessing the fields by tabbing with the keyboard.

You can prevent the default styling associated with the fieldset element with the following CSS:

fieldset {
  margin: 0;
  padding: 0;
  border: none;
}

See this stackblitz for a demo.

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.