2

I have few buttons and div elements. divs are hidden. I want to reveal the specific div, when the specific button is clicked.

For example: You click first button - only first div appears (appears by changing his class).

Code: app.component.html

<button class="BUTTON" (click)="isHidden = !isHidden">
</button>
<div class="ELEMENTTOSHOW" [ngClass]="{ 'subMenuShow':isHidden }">first
</div>

<button class="BUTTON" (click)="isHidden = !isHidden">
</button>
<div class="ELEMENTTOSHOW" [ngClass]="{ 'subMenuShow':isHidden }">second
</div>

app.component.ts

export class AppComponent {
  isHidden = false;
}

This solution is wrong because when I click e.g. first BUTTON then both divs ELEMENTTOSHOW appear. It shouldnt work like that, it should only work on a specific div.

Any help appreciated.

Edit: here is my code preview on plunker:

https://plnkr.co/edit/BQKanrtuoADClGLVSdBC?p=preview

Its not my whole app, only the mentioned, VERY simplified part.

I have added AngularJS tag, because you can give me a solution related with angular1 and I will just rewrite it into angular2.

2
  • Could you provide a Plunker with the current state of your application? In the code snippets presented above there aren't any actual buttons, only <span> elements with a "button" class. Commented Nov 20, 2016 at 20:08
  • @TudorCiotlos plnkr.co/edit/BQKanrtuoADClGLVSdBC?p=preview Commented Nov 20, 2016 at 20:48

1 Answer 1

1

I'd restructure it like this:

HTML

<button class="BUTTON" (click)="isHiddenfirst = !isHiddenfirst">
</button>
<div *ngIf="isHiddenfirst" class="ELEMENTTOSHOW subMenuShow">
    first
</div>

<button class="BUTTON" (click)="isHiddensecond = !isHiddensecond">
</button>
<div *ngIf="isHiddensecond" class="ELEMENTTOSHOW subMenuShow">
    second
</div>

TS

import {Component} from 'angular2/core';

@Component({
  // Declare the tag name in index.html to where the component attaches
  selector: 'hello-world',

  // Location of the template for this component
  templateUrl: 'src/hello_world.html',
  styleUrls: ['src/hello_world.css']
})
export class HelloWorld {

isHiddenfirst = false;
ishiddensecond = false;
}

https://plnkr.co/edit/BOLCoRY0SmLe8GZT3PUG?p=preview

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

3 Comments

Thanks, but it's not really dynamic. Upvote my question because I need 15 rep points to upvote your answer :P
I still need 4 rep to upvote, when I get it I will upvote u. If nobody else answers I will check it as the best answer.
I created another solution so that it's more dynamic: plnkr.co/edit/dDIwSH6BFwIR0ZI10vlO?p=preview I think your issue is that they are both appearing when isHidden is true because they're both set to show when it's true and hide when it's false

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.