I'm having a problem hiding and showing an element depending of a boolean variable in Angular 2.
this is the code for the div to show and hide:
<div *ngIf="edited==true" class="alert alert-success alert-dismissible fade in" role="alert">
<strong>List Saved!</strong> Your changes has been saved.
</div>
the variable is "edited" and it's stored in my component:
export class AppComponent implements OnInit{
(...)
public edited = false;
(...)
saveTodos(): void {
//show box msg
this.edited = true;
//wait 3 Seconds and hide
setTimeout(function() {
this.edited = false;
console.log(this.edited);
}, 3000);
}
}
The element is hidden, when saveTodos function starts, the element is shown, but after 3 seconds, even if the variable come back to be false, the element does not hide. Why?
This question is related to
typescript
angular
We can do it by using the below code snippet..
Angular Code:
export class AppComponent {
toggleShowHide: string = "visible";
}
HTML Template:
Enter text to hide or show item in bellow:
<input type="text" [(ngModel)]="toggleShowHide">
<br>
Toggle Show/hide:
<div [style.visibility]="toggleShowHide">
Final Release Angular 2!
</div>
Just add bind(this) in your setTimeout function it will start working
setTimeout(function() {
this.edited = false;
console.log(this.edited);
}.bind(this), 3000);
and in HTML change
<div *ngIf="edited==true" class="alert alert-success alert-dismissible fade in" role="alert">
<strong>List Saved!</strong> Your changes has been saved.
</div>
To
<div *ngIf="edited" class="alert alert-success alert-dismissible fade in" role="alert">
<strong>List Saved!</strong> Your changes has been saved.
</div>
Depending on your needs, *ngIf
or [ngClass]="{hide_element: item.hidden}"
where CSS class hide_element
is { display: none; }
*ngIf
can cause issues if you're changing state variables *ngIf
is removing, in those cases using CSS display: none;
is required.
There are two options depending what you want to achieve :
You can use the hidden directive to show or hide an element
<div [hidden]="!edited" class="alert alert-success box-msg" role="alert">
<strong>List Saved!</strong> Your changes has been saved.
</div>
You can use the ngIf control directive to add or remove the element. This is different of the hidden directive because it does not show / hide the element, but it add / remove from the DOM. You can loose unsaved data of the element. It can be the better choice for an edit component that is cancelled.
<div *ngIf="edited" class="alert alert-success box-msg" role="alert">
<strong>List Saved!</strong> Your changes has been saved.
</div>
For you problem of change after 3 seconds, it can be due to incompatibility with setTimeout. Did you include angular2-polyfills.js library in your page ?
For child component to show I was using *ngif="selectedState == 1"
Instead of that I used [hidden]="selectedState!=1"
It worked for me.. loading the child component properly and after hide and un-hide child component was not undefined after using this.
This is a good use case for a directive. Something like this is surprisingly useful.
@Directive({
selector: '[removeAfter]'
})
export class RemoveAfter {
constructor(readonly element: ElementRef<HTMLElement>) { }
/**
* Removes the attributed element after the specified number of milliseconds.
* @default `1000`
*/
@Input() removeAfter = 1000;
ngOnInit() {
setTimeout(() => {
this.element.nativeElement.remove();
}, this.removeAfter);
}
}
When you don't care about removing the Html Dom-Element, use *ngIf.
Otherwise, use this:
<div [style.visibility]="(numberOfUnreadAlerts == 0) ? 'hidden' : 'visible' ">
COUNTER: {{numberOfUnreadAlerts}}
</div>
@inoabrian solution above worked for me. I ran into a situation where I would refresh my page and my hidden element would reappear on my page. Here's what I did to resolve it.
export class FooterComponent implements OnInit {
public showJoinTodayBtn: boolean = null;
ngOnInit() {
if (condition is true) {
this.showJoinTodayBtn = true;
} else {
this.showJoinTodayBtn = false;
}
}
Source: Stackoverflow.com