【发布时间】:2016-02-15 16:50:58
【问题描述】:
我在我的 Angular 应用程序上连接了一个状态栏,目的是当向服务器发出请求时,状态栏将显示响应消息,将有一个背景颜色来表示成功或错误,如果成功几秒钟后隐藏。
我看到的是,在加载页面后第一次运行此逻辑时,动画不会运行(淡入和定时淡出都无法运行),但前提是状态栏元素最初是隐藏的,如果我在页面加载时将 ng-show 变量设置为 true,所有动画都会按预期工作。
我确实通过 chrome 的开发人员工具检查了源代码,在第一次运行期间,在动画应该完成后,div 具有这些类 alert-bar ng-hide-remove ng-hide-remove-active alert-bar-success ng-animate ng-hide。当动画确实起作用时,唯一存在的类是alert-bar alert-bar-success ng-animate ng-hide。
HTML:
<div class="alert-bar" ng-show="response.show" ng-class="(response.result == true) ? 'alert-bar-success' : 'alert-bar-danger'">
<div class="container">
<label>Message: {{response.message}}</label>
</div>
</div>
CSS:
.alert-bar {
width: 100%;
margin-top: -20px;
margin-bottom: 20px;
}
.alert-bar-success {
background-color: #5cb85c;
border-color: #4cae4c;
color: #ffffff;
}
.alert-bar-danger {
color: #ffffff;
background-color: #d9534f;
border-color: #d43f3a;
}
.alert-bar.ng-hide-add, .alert-bar.ng-hide-remove {
-webkit-transition:all linear 0.3s;
-moz-transition:all linear 0.3s;
-o-transition:all linear 0.3s;
transition:all linear 0.3s;
display:block!important;
}
.alert-bar.ng-hide-add.ng-hide-add-active,
.alert-bar.ng-hide-remove {
opacity:0;
}
.alert-bar.ng-hide-add,
.alert-bar.ng-hide-remove.ng-hide-remove-active {
opacity:1;
}
控制器:
controllers.controller("AppController", ['$scope', '$timeout', function($scope, $timeout) {
$scope.response = {};
$scope.response.received = function(message, result) {
$scope.response.message = message;
$scope.response.result = result;
$scope.response.show = true;
if (result == true) {
$timeout(function () {
$scope.response.show = false;
}, 4000);
}
};
}]);
【问题讨论】:
标签: angularjs ng-animate