javascript - Angular2: How to fade out box-msg after a while which is displayed onClick - Stack Overflow

Might be a silly question but am new to angular and still learning.Am showing a success message on butt

Might be a silly question but am new to angular and still learning.

Am showing a success message on button click which I need to fade out after few seconds.

<div *ngIf="hideSharedLinkCopyMessage" class="alert alert-success box-msg " role="alert">
    <strong>Link Generated!</strong> Your sharable link is copied to clipboard.
</div>

Now, am using alert-success and box-msg classes. I tried to add fadeOut class as well but that didn't worked.

hideSharedLinkCopyMessage is set to true when the button in clicked. Initially it is set to false How can I fade this message out after few seconds?

Might be a silly question but am new to angular and still learning.

Am showing a success message on button click which I need to fade out after few seconds.

<div *ngIf="hideSharedLinkCopyMessage" class="alert alert-success box-msg " role="alert">
    <strong>Link Generated!</strong> Your sharable link is copied to clipboard.
</div>

Now, am using alert-success and box-msg classes. I tried to add fadeOut class as well but that didn't worked.

hideSharedLinkCopyMessage is set to true when the button in clicked. Initially it is set to false How can I fade this message out after few seconds?

Share Improve this question edited May 10, 2018 at 8:07 Manish Balodia 1,8772 gold badges24 silver badges38 bronze badges asked May 10, 2018 at 6:53 roger_thatroger_that 9,80119 gold badges70 silver badges110 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Add a timeout function after setting true hideSharedLinkCopyMessage. In the next example the link will fade out after 2 seconds;

FadeOutLink() {
    setTimeout( () => {
          this.hideSharedLinkCopyMessage = false;
        }, 2000);
   }

An other option and politer is to use Angular Materials and import the Snackbar ponent. Is really easy to use and you can customize it as you want.

Normal Fade out will not work with *ngif because with *ngif, the element is directly removed from the DOM.

You need to use ngClass to add/remove a class(fadeout) which will have the fade out effect and then use a timeout to turn your variable hideSharedLinkCopyMessage to true and element will be removed from DOM.

Controller:

<your method to remove alert> () {
    // add class fadeOut here
    setTimeout( () => {
      this.hideSharedLinkCopyMessage = false;
    }, 3000);      
}

Success Message will disappear after 4 sec in angular7

  export class UnsubscribeComponent implements OnInit {
  hideSuccessMessage = false;

  FadeOutSuccessMsg() {
    setTimeout( () => {
           this.hideSuccessMessage = true;
        }, 4000);
   }

   ponent.html -
   ------------------
          <div  *ngIf="!hideSuccessMessage">
            <div class="col-12">
              <p class="alert alert-success">
                  <strong [ngClass] ="FadeOutSuccessMsg()" >You are successfully 
                  unsubscribe from this email service.</strong>                 
              </p>
            </div>
          </div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744201286a4562890.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信