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 ?
You should use the *ngIf Directive
<div *ngIf="edited" class="alert alert-success box-msg" role="alert">
<strong>List Saved!</strong> Your changes has been saved.
</div>
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);
}.bind(this), 3000);
}
}
Update: you are missing the reference to the outer scope when you are inside the Timeout callback.
so add the .bind(this) like I added Above
Q : edited is a global variable. What would be your approach within a *ngFor-loop? – Blauhirn
A : I would add edit as a property to the object I am iterating over.
<div *ngFor="let obj of listOfObjects" *ngIf="obj.edited" class="alert alert-success box-msg" role="alert">
<strong>List Saved!</strong> Your changes has been saved.
</div>
export class AppComponent implements OnInit{
public listOfObjects = [
{
name : 'obj - 1',
edit : false
},
{
name : 'obj - 2',
edit : false
},
{
name : 'obj - 2',
edit : false
}
];
saveTodos(): void {
//show box msg
this.edited = true;
//wait 3 Seconds and hide
setTimeout(function() {
this.edited = false;
console.log(this.edited);
}.bind(this), 3000);
}
}
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>
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 an Angular 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.
*/
@Input() removeAfter: number;
ngOnInit() {
setTimeout(() => {
this.element.nativeElement.remove();
}, this.removeAfter);
}
}
Usage:
<div [removeAfter]="3000">Removed after 3 seconds</div>
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With