gpt4 book ai didi

angularjs - 将状态类应用于 ui-view 以允许不同的 ng 动画

转载 作者:行者123 更新时间:2023-12-02 22:43:52 26 4
gpt4 key购买 nike

我需要根据当前状态使用ui-view应用不同的动画。继this question..之后

我有以下代码(编辑:参见 plunker preview )

<section ui-view ng-class="stateClass"></section>

stateClass 应用于每个 Controller ,例如:

.controller('loginController', function($scope, $state) {
// Set state class name
$scope.stateClass = 'slide-left';
console.log($scope);

这有效并且类被添加得很好 - 但动画不会启动。

如果我将 ui-view 代码更新为:

<section ui-view class="slide-left"></section>

对于硬编码的类,这是可行的(但现在我无法应用不同的动画)。

ng-class 是否在 ng-enter 之后添加?有人可以建议如何实现动画吗?

edit>> 奇怪的是 ng-leave 动画效果很好。但 css 仍然不适用于 ng-enter

最佳答案

将以下指令添加到您的 ui-view state-class:

.directive('stateClass', ['$state', function($state) {
return {
link: function($scope, $element, $attrs) {
var stateName = $state.current.name || 'init',
normalizedStateName = 'state-' + stateName.replace(/\./g, '-');
$element.addClass(normalizedStateName);
}
}
}]);

在 ng-enter 上,对于新创建的元素,它将添加当前状态名称(ui-view 转换到的状态)。对于即将消失的元素,其状态保持与之前创建时相同。

例如:

main状态到modal状态的转换:

第 1 阶段:main 状态处于事件状态:

<ui-view state-class class="state-main ng-scope"></ui-view>

阶段 2:运行 $state.go('modal');

<ui-view state-class class="state-modal ng-animate ng-enter ng-enter-active"></ui-view>
<ui-view state-class class="state-main ng-animate ng-leave ng-leave-active"></ui-view>

第 3 阶段:模态 状态处于事件状态

<ui-view state-class class="state-modal"></ui-view>

关于angularjs - 将状态类应用于 ui-view 以允许不同的 ng 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29091137/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com