javascript - 将 ui-router 与 View 转换一起使用

标签 javascript angularjs

我正在使用 ui-router 并且我正在尝试让 View 转换工作,即在 View 更改时设置动画。我在下面尝试过,但在 View 更改时没有看到任何动画,为什么?

HTML:

正在加载...

我js的相关部分:

// Initialize the main module
app.run(['$rootScope', '$location', '$window', function ($rootScope, $location, $window) {

    'use strict';

    /**
     * Helper method for main page transitions. Useful for specifying a new page partial and an arbitrary transition.
     * @param  {String} path               The root-relative url for the new route
     * @param  {String} pageAnimationClass A classname defining the desired page transition
     */
    $rootScope.go = function (path, pageAnimationClass) {


        if (typeof(pageAnimationClass) === 'undefined') { // Use a default, your choice
            $rootScope.pageAnimationClass = 'crossFade';
        }

        else { // Use the specified animation
            $rootScope.pageAnimationClass = pageAnimationClass;
        }

        if (path === 'back') { // Allow a 'back' keyword to go to previous page
            $window.history.back();
        }

        else { // Go to the specified path
            $location.path(path);
        }
    };
}]);

app.config(function ($stateProvider, $urlRouterProvider, RestangularProvider) {
    // For any unmatched url, send to /route1
    $urlRouterProvider.otherwise("/");
    $stateProvider
        .state('index', {

            url: "/",
            templateUrl: "/static/html/partials/test1.html",
            controller: "TestList"
        })

        .state('test', {

            url: "/test",
            templateUrl: "/static/html/partials/test.html",
            controller: "TestCtrl"
        })



})

CSS:

/* Transitions */

/* Default Enter/Leave */
.ng-enter,
.ng-leave {
    transition-timing-function: ease;
    transition-duration: 250ms;
    transition-property: opacity;
}

.ng-enter {
    opacity: 0;
}

.ng-enter.ng-enter-active {
    opacity: 1;
}

.ng-leave {
    opacity: 1;
}

.ng-leave.ng-leave-active {
    opacity: 0;
}

/* crossFade */
.crossFade.ng-enter {
    transition-duration: 100ms;
    opacity: 0;
}

.crossFade.ng-enter.ng-enter-active {
    opacity: 1;
}

.crossFade.ng-leave {
    transition-duration: 100ms;
    opacity: 1;
}

.crossFade.ng-leave.ng-leave-active {
    opacity: 0;
}

/* slideRight */
.slideRight {
    opacity: 1 !important;
}

.slideRight.ng-enter {
    transition-property: none;
    transform: translate3d(-100%,0,0);
}

.slideRight.ng-enter.ng-enter-active {
    transition-property: all;
    transform: translate3d(0,0,0);
}

.slideRight.ng-leave {
    transition-property: all;
    transform: translate3d(0,0,0);
}

.slideRight.ng-leave.ng-leave-active {
    transition-property: all;
    transform: translate3d(100%,0,0);
}

/* slideLeft */
.slideLeft {
    opacity: 1 !important;
}

.slideLeft.ng-enter {
    transition-property: none;
    transform: translate3d(100%,0,0);
}

.slideLeft.ng-enter.ng-enter-active {
    transition-property: all;
    transform: translate3d(0,0,0);
}

.slideLeft.ng-leave {
    transition-property: all;
    transform: translate3d(0,0,0);
}

.slideLeft.ng-leave.ng-leave-active {
    transition-property: all;
    transform: translate3d(-100%,0,0);
}

/* slideDown */
.slideDown {

}

.slideDown.ng-enter {
    transition-property: none;
    transform: translate3d(0,-100%,0);
}

.slideDown.ng-enter.ng-enter-active {
    transition-property: all;
    transform: translate3d(0,0,0);
}

.slideDown.ng-leave {
    transition-property: all;
    transform: translate3d(0,0,0);
}

.slideDown.ng-leave.ng-leave-active {
    transition-property: all;
    transform: translate3d(0,100%,0);
}

/* slideUp */
.slideUp {
    opacity: 1 !important;
}

.slideUp.ng-enter {
    transition-property: none;
    transform: translate3d(0,100%,0);
}

.slideUp.ng-enter.ng-enter-active {
    transition-property: all;
    transform: translate3d(0,0,0);
}

.slideUp.ng-leave {
    transition-property: all;
    transform: translate3d(0,0,0);
}

.slideUp.ng-leave.ng-leave-active {
    transition-property: all;
    transform: translate3d(0,-100%,0);

最佳答案

下面是我如何在状态之间实现简单的 View 转换。

HTML:过渡状态

<div class="row checkout-process">
<section class="col-sm-8 col-md-8 col-lg-8">
    <article class="shopping-cart" ui-view="shopping-cart" autoscroll="false"></article>
    <article class="order-confirmation" ui-view="order-confirmation" autoscroll="false"></article>
    <article class="thank-you" ui-view="thank-you" autoscroll="false"></article>
</section>

<section class="col-sm-4 col-md-4 col-lg-4">
    <kx-order-total></kx-order-total>
</section>

HTML: closed-state.html

<article class="col-sm-12 col-md-12 col-lg-12 next-state">
    <a ui-sref="{{state.name}}">
        <i class="fa fa-plus"></i> &nbsp;
        {{page.name}}
    </a>
</article>

CSS:

.shopping-cart[ui-view], .order-confirmation[ui-view], .thank-you[ui-view] {
  overflow: hidden;
}

.shopping-cart[ui-view].ng-enter, .order-confirmation[ui-view].ng-enter, .thank-you[ui-view].ng-enter {
  height: 0px;
  @include transition(height .35s ease-in-out);
  @include transition-delay(.35s);
}

.shopping-cart[ui-view].ng-enter-active, .order-confirmation[ui-view].ng-enter-active, .thank-you[ui-view].ng-enter-active {
  height: 200px;
}

.shopping-cart[ui-view].ng-leave, .order-confirmation[ui-view].ng-leave, .thank-you[ui-view].ng-leave {
  @include transition(all .35s ease-in-out);
  height: 200px;
}

.shopping-cart[ui-view].ng-leave-active, .order-confirmation[ui-view].ng-leave-active, .thank-you[ui-view].ng-leave-active {
  height: 0px;
}

最后是我的过渡状态的相关部分。

            ////////////////////////
            //Order Checkout State//
            ////////////////////////

            .state('home.checkout', {
                url: 'checkout',
                views: {
                    '@home': {
                        templateUrl: 'views/partials/generic/checkout-process/order-checkout-root.html'
                    }
                }
            })

            .state('home.checkout.shoppingcart', {
                url: '^/shoppingcart',
                views: {
                    'shopping-cart@home.checkout': {
                        templateUrl: 'views/partials/generic/checkout-process/shoppingcart/shopping-cart-partial.html',
                        controller: 'ShoppingCartController'
                    },
                    'order-confirmation@home.checkout' : {
                        templateUrl: 'views/partials/generic/checkout-process/closed-state.html',
                        controller: function($scope) {
                            $scope.page = {name: 'Order Confirmation'};
                            $scope.state = {name: 'home.checkout.confirm'};
                        }
                    },
                    'thank-you@home.checkout' : {
                        templateUrl: 'views/partials/generic/checkout-process/closed-state.html',
                        controller: function($scope) {
                            $scope.page = {name: 'Order Completion'};
                            $scope.state = {name: 'home.checkout.thankyou'};
                        }
                    }
                }
            })

            .state('home.checkout.confirm', {
                url: '/confirmation',
                views: {
                    'shopping-cart@home.checkout': {
                        templateUrl: 'views/partials/generic/checkout-process/closed-state.html',
                        controller: function($scope) {
                            $scope.page = {name: 'Shopping Cart'};
                            $scope.state = {name: 'home.checkout.shoppingcart'};
                        }
                    },
                    'order-confirmation@home.checkout': {
                        templateUrl: '../views/partials/generic/checkout-process/confirmation/order-confirmation-partial.html',
                        controller: 'OrderConfirmationController'
                    },
                    'thank-you@home.checkout' : {
                        templateUrl: 'views/partials/generic/checkout-process/closed-state.html',
                        controller: function($scope) {
                            $scope.page = {name: 'Order Completion'};
                            $scope.state = {name: 'home.checkout.thankyou'};
                        }
                    }
                }
            })

            .state('home.checkout.thankyou', {
                url: '/thankyou',
                parent: 'home.checkout.confirm',
                views: {
                    'shopping-cart@home.checkout': {
                        templateUrl: 'views/partials/generic/checkout-process/closed-state.html',
                        controller: function($scope) {
                            $scope.page = {name: 'Shopping Cart'};
                            $scope.state = {name: 'home.checkout.shoppingcart'};
                        }
                    },
                    'order-confirmation@home.checkout' : {
                        templateUrl: 'views/partials/generic/checkout-process/closed-state.html',
                        controller: function($scope) {
                            $scope.page = {name: 'Order Confirmation'};
                            $scope.state = {name: 'home.checkout.confirm'};
                        }
                    },
                    'thank-you@home.checkout': {
                        templateUrl: '../views/partials/generic/checkout-process/thank-you/thank-you-partial.html',
                        controller: 'OrderConfirmationController'
                    }
                }
            });

这个例子有点复杂,因为我在父“home.checkout”的子状态之间转换

如果你想要一个很好的例子,请查看 ui-router伙计们的例子。这是让我前进的动力,也是一种非常简单的学习方法。

关于javascript - 将 ui-router 与 View 转换一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21005880/

相关文章:

javascript - 在 jQuery 中, `this.element` 和 `this.element[/* some number */]` 有什么区别?

javascript - Discord.js 14 - 当成员加入时无法将图像发送到特定 channel

javascript - CSS 和 PHP 运行 exec 并返回原始页面

html - 固定位置导航栏/标题有效,但内容不会在标题下滚动

angularjs - 使用 Angular UI Router 在 View 中进行 ng-click 时,范围变量不会更新

javascript - 在 tumblr 中设置一个帖子标签?

javascript - 如何使用选择表单从 AngularJS 中的 JSON 获取属性

android - 自动完成在移动设备上不起作用

javascript - AngularJS 中的后台服务

javascript - 如何将光滑 slider 直接对齐到容器中底部元素的上方?