1

I have a custom element that I just want to show when I click a button. Then, when I click that button, I want to do something like...:

<my-element [hidden]="switchHidden()"></my-element>

... so if it's in false, it now will be true, and viceversa. A very typical approach.

But I don't know how I can pass to my function "switchHidden()" the value of the "hidden" attribute. How can I do this, so I can check whether it is true or false?

Thank you!

1
  • Is the button you want to click to hide show custom element, inside custom element or outside? Commented Oct 31, 2016 at 15:49

2 Answers 2

2

Don't use hidden attribute, use:

<my-element *ngIf="switchHidden"></my-element>

http://angularjs.blogspot.ba/2016/04/5-rookie-mistakes-to-avoid-with-angular.html

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

1 Comment

That seems kinda stupid (sorry not meaning to be offensive), display:none, is so much more lightweight than creating and destroying DOM elements, if you have CSS problems, you can always add your own attribute CSS [hidden] { display: none !important; }
1

PLUNKER Demo

Just have a variable in your component that is a boolean for when you want to show and hide it.

let showElement: Boolean = true;

Then when you want to toggle it on/off with a button put this in the click.

<button (click)="showElement = !showElement">Toggle</button>

and that will flip the value of showElement every time the user clicks the button. So if showElement is true, when the user clicks the button it will make showElement false and thus hidding your element. Vise versa for when showElement is false, the user clicks the button making it true and thus showing your element.

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.