How to add animation to angularjs uib-alert directive

橙三吉。 提交于 2020-01-03 17:06:27

问题


I want to add fade-in animation for new alerts pushed into array and fade-out for dismissed alerts.

Alerts are dismissed automatically after 5 seconds.

I've already included angular-animate ui-bootstrap and ui-bootstrap-tpls libraries.

How can i get these animations working?

See Demo: http://plnkr.co/edit/ecbCA7h2zVA4XnvUHfFX?p=preview

HTML

<html ng-app="myApp">

  <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
    <script src="https://code.angularjs.org/1.5.0/angular-animate.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/1.1.2/ui-bootstrap.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/1.1.2/ui-bootstrap-tpls.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-controller="MainController">
    <h1>Alert With Animation</h1>
    <uib-alert 
      ng-repeat="alert in alerts" 
      type="{{alert.type}}" 
      dismiss-on-timeout="5000" 
      close="alerts.splice($index, 1);">{{alert.msg}}
    </uib-alert>
    <input type='text' ng-model='msg' />
    <button ng-click="addAlert(msg,'danger')">Add Alert</button>
  </body>

</html>

SCRIPT

(function() {

  var app = angular.module("myApp", ['ui.bootstrap', 'ngAnimate']);

  app.controller("MainController", function($scope) {

    $scope.alerts = [];

    $scope.msg = "No Animation!";

    $scope.addAlert = function(msg, type) {
      $scope.alerts.push({
        msg: msg,
        type: type
      });
    };

  });

}());

回答1:


Answer is based on Pankaj's comment.

Which lead me to this article: http://www.yearofmoo.com/2013/08/remastered-animation-in-angularjs-1-2.html

  • added class='repeat-item' to <uib-alert>..</uib-alert> element.
  • added proper stylings for animation effects.

    <style type="text/css">
    .repeat-item.ng-enter,
    .repeat-item.ng-leave {
      -webkit-transition: 0.5s linear all;
      transition: 0.5s linear all;
    }
    
    .repeat-item.ng-enter,
    .repeat-item.ng-leave.ng-leave-active {
      opacity: 0;
    }
    
    .repeat-item.ng-leave,
    .repeat-item.ng-enter.ng-enter-active {
      opacity: 1;
    }
    </style>
    

See working demo: https://plnkr.co/edit/CK2lV4mBVGs8q5gyYklE?p=preview

One little glitch

When you click to add an alert for the very first time, there is no fade-in animation. After that, everything seems OK.



来源:https://stackoverflow.com/questions/35334601/how-to-add-animation-to-angularjs-uib-alert-directive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!