简体   繁体   English

Angular.js:使用routeparams和重新加载

[英]Angularjs: Using routeparams and reload

My code is: 我的代码是:

supportByProductApp.config(['$routeProvider',
    function($routeProvider) {
        //Setting up HTML pages and controllers depending upon the suffix in the URL
        $routeProvider.
        when('/all_products/:alphabet?', {
            templateUrl: '/etc/designs/help/clientlibs/support/support-hub/allproducts.html',
            controller: 'CategoryListCtrl',
            resolve: {
                categoryID: function() {
                    return 'all_products';
                }
            }
        })
        .....

On clicking a link, I invoke following code: 单击链接后,我将调用以下代码:

$scope.alphabetClicked = function(albhabet) {
    $location.path('/all_products/' + albhabet);
    $route.reload();
}

The problem I have is that when doing the reload of ng-view, the controller would created 3 ng-view elements and then it will destroy 2 of them. 我的问题是,在重新加载ng-view时,控制器会创建3个ng-view元素,然后它将销毁其中的2个。 So finally I do see 1 ng-view but intermediately, I see 3 ng-view in the html. 所以最后我确实看到了1 ng视图,但是在中间,我看到了html中的3 ng视图。 In my html body, I have only 1 ng-view. 在我的html正文中,我只有1 ng视图。 The 2 extra ng-view contain old html structure and 1 of them contains new html structure (as determined by the logic written in allproducts.html). 另外2个ng-view包含旧的html结构,其中1个包含新的html结构(由allproducts.html中编写的逻辑确定)。

PS categoryID is a variable which I use inside my all_products.html code Since my code is server dependent, its really hard to write a plunker for it. PS categoryID是我在all_products.html代码中使用的变量。由于我的代码与服务器有关,因此很难为其编写代码。

Update: I have created a similar plunker here : http://plnkr.co/edit/FiNwsMIugFznaSwCqUDc?p=preview 更新:我在这里创建了一个类似的插件: http ://plnkr.co/edit/FiNwsMIugFznaSwCqUDc?p=preview

Steps to reproduce: 重现步骤:

1) Click on the first show details hyperlink. 1)单击第一个显示详细信息超链接。 The animation comes fine with single ng-view 单个ng-view的动画效果很好

2) Click on second show details hyperlink. 2)单击第二个显示详细信息超链接。 The first ng-view is still visible while the other one has already started to appear. 第一个ng视图仍然可见,而另一个视图已开始出现。

If we see the Web browser Inspector, we can see two ng-view elements are created. 如果看到Web浏览器Inspector,则可以看到创建了两个ng-view元素。 Animation/ng-animate has nothing to do with multiple ng-view. 动画/ ng动画与多个ng-view无关。 We can remove animation and still see multiple ng-view are there on click of hyperlink in Web browser Inspector (they appear and disappear quickly if we remove animation). 我们可以删除动画,但在Web浏览器检查器中单击超链接时仍会看到多个ng-view(如果删除动画,它们会迅速消失)。 In my app, im going to use animations and hence this is of concern to me. 在我的应用程序中,即时消息将使用动画,因此我对此感到担忧。

The ng-view behavior in the animation is normal, as angular needs an element for enter animation, and an element for leave animation. 动画中的ng-view行为是正常的,因为angle需要一个元素用于输入动画,一个元素用于离开动画。 To prevent the jump you just have to synchronize the timing, and use position absolute to prevent the entering element from pushing the leaving element down. 为了防止跳跃,您只需要同步定时,并使用绝对位置来防止进入元素将离开元素下推。

The basic idea comes from this article . 基本思想来自本文 I've modified it to have a fade in and then fade out, and not together - fiddle . 我已经对其进行了修改,使其先淡入淡出,然后淡出,而不是一起摆弄

css: CSS:

h2 { /** prevent h2 default margin from causing the leaving ngView to jump down when setting position: absolute **/
  margin-top: 0;
}

js: JS:

sampleApp.animation('.content',
  function() {
    return {
      enter: function(element, done) {
        var delay = $('.content').length === 2 ? 600 : 0; // if there are 2 .content (ngView) delay entrance, so the 1st can leave
        $(element).css({
          opacity: 0 // set the stage
        });
        $(element).delay(delay).animate({ // animate the opacity with delay if needed
          opacity: 1
        }, 600, done);
      },
      leave: function(element, done) {

        $(element).css({
          position: 'absolute', // use position absolute so the element won't jump down
          opacity: 1 // set the stage
        });
        $(element).animate({
          opacity: 0
        }, 600, done);

      }
    }
  }
)

Below is the code which lets you generate only one ng-view. 以下是仅生成一个ng-view的代码。

sampleApp.animation('.content', function() {
  return {
    enter: function(element, done) {
  element.css('display', 'none');
  jQuery(element).hide().fadeIn(2200, done);
  return function() {
    //element.stop();
  }
},
leave: function(element, done) {
  jQuery(element).fadeOut(0, done);//note this will not show animation on leave
  return function() {
    //element.stop();
  }
}
  }
});    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM