Click here to Skip to main content
16,018,006 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
See more:
I tried to Hide and Show the Div having "Hello Brother"on button click event using Angular2, but my code is not working

please help me how i can show/hide the div on button click event.

What I have tried:

HTML
import {Component} from 'angular2/core';
@Component({
    selector: 'my-app',
    template: `<div><h1>Hello world</h1>
<button (click)="clicked()"> Click Me</button>
<div ng-if="show">
<h2>Hello Brother</h2>
</div></div>`
})
export class BaseComponent {

    show: boolean = false;
    clicked() {

        //alert("test");

        this.show = !this.show;
        //alert("test");
    }

}
Posted
Updated 25-Mar-16 22:14pm
v2
Comments
Anisuzzaman Sumon 22-Mar-16 23:47pm    
Not a well organized question .Improve your question it makes no sense to me

there are is ng-show and ng-hide
https://docs.angularjs.org/api/ng/directive/ngShow[^]
https://docs.angularjs.org/api/ng/directive/ngHide[^]

This are the websites if you are using angular 1 but no 2
 
Share this answer
 
Comments
Konstantin A. Magg 26-Mar-16 4:16am    
Hi there,
as far as I understand the question (`import {Component} from 'angular2/core'`), it is clearly about angular 2.
Hi Sanjit,

did you already check out the Angular 2 tutorial on angular.io? They cover a lot of typical examples and they explain things very clearly.

In this case, take a look at the Master/Detail part[^] and search for "Hide the empty detail with ngIf"

This code only shows a div, if the selectedHero property is set.
HTML
<!-- template -->
<div *ngIf="selectedHero"></div>


JavaScript
// controller
onSelect(hero: Hero) { this.selectedHero = hero; }


Hope this helps. If you have further questions, feel free to ask.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900