NG-格:为什么afterSelectionChange()调用两次? [英] ng-grid : Why is afterSelectionChange() called twice?

查看:541
本文介绍了NG-格:为什么afterSelectionChange()调用两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不喜欢的答案,<一个href=\"http://stackoverflow.com/questions/18504761/angular-nggrid-select-row-on-page-load/19391176#comment35828136_19391176\">this问题。在我看来,它有一个不好的code气味。

I did not like the answer to this question. To my mind, it has a bad code smell.

我是新来的角,所以我想我可以学到一些东西,如果我创建了一个普拉克帮助那些提问。

I am new to Angular, so I thought I could learn something if I created a Plunk to help that questioner.

我的普拉克是 http://plnkr.co/edit/fJcew7cGCPzJ0nhIQXjg?p= preVIEW ,但有两件事情,我不明白。

My Plunk is http://plnkr.co/edit/fJcew7cGCPzJ0nhIQXjg?p=preview, but there two things which I don't understand.

请注意,我指定了NG-网格的数据两种方式 - 在一下跌中风和逐行。评论一出&安培;取消对其他玩弄它。

Please note that I assigned the ng-grid's data two ways - at one fell stroke and row by row. Comment one out & uncomment the other to play around with it.

和一定要preSS F12,看看开发商的控制台日志消息。

And be sure to press F12 to see the developer's console log messages.

1)为什么我获得两个ngGridEventData事件?

1) why am I getting TWO ngGridEventData events?

2)为什么,当我推按行()行我不能让每推1 ngGridEventData事件()?
   我想实现主/从/更详细的3个网格(没有与当前的问题做),现在已经被谷歌上搜索了好几天。许多人似乎相信他们会得到多个ngGridEventData事件,并想知道浩检测网格完全渲染(因此我普拉克检查)。

2) why, when I push() row by row am I not getting one ngGridEventData event per push() ? I am trying to implement master/detail/even more detailed with 3 grids (nothing to do with the current question) and have been googling for a few days now. Many people seem believe that they will get multiple ngGridEventData events and want to know ho to detect that the grid is fully rendered (hence the check in my Plunk).

总之,任何人都可以详细ngGridEventData是如何工作的解释?谢谢;你的回答将帮助了很多人。

In short, can anyone explain in detail how ngGridEventData works? Thanks; your answer will help a lot of people.

推荐答案

在最近的答案<一个href=\"http://stackoverflow.com/questions/23116683/persist-selected-rows-the-correct-way/23134977#23134977\">here我发现这个奇怪的行为一些学习收获。看来,这个程序被调用2次的原因,我不能真正理解(缓存可能?)。

In a recent Answer here I showed some learnings about this strange behavior. It seems that this routine is called 2 times for reasons I can't really understand (caching maybe?).

您能得到这个工作,如果你才反应过来时,数据对象是一个克隆:

You can get this to work if you only react when the data-object is a clone:

  if (data.selected === true) {
    if (data.isClone) {
      numSelectionChanges = numSelectionChanges + 1;
      console.log("afterSelectionChange", 'Selection has chanegd ' + numSelectionChanges + ' times');
      console.table(data);
    }
  }

试试看吧,如果你找出为什么出现这种情况,请让我知道。

Give it a try, and if you find out why this happens please let me know.

更多结果更新时间:

看一看这个 Plunker

我已经包含在plunker直接NG-grid.js脚本。
如果你看看行3344-3349,你会发现这片code的:

I have included the ng-grid.js script directly in the plunker. If you look at rows 3344-3349 you will find this piece of code:

$scope.$on('$destroy', $scope.$parent.$watch(options.data, dataWatcher));
$scope.$on('$destroy', $scope.$parent.$watch(options.data + '.length', function() {
     dataWatcher($scope.$eval(options.data));
     $scope.adjustScrollTop(grid.$viewport.scrollTop(), true);
}));

正如你可以看到有应用它看起来像从改变我剩2观察家。的(虽然我不知道,我是哑巴和那些家伙是聪明)的。但是,如果我评论的第一行出一切似乎都如预期工作。

As you can see the there are two watchers applied which looks like a leftover from changes to me. (Although i'm not sure, with me being dumb and those guys being clever). However, if I comment the first line out everything seems to work as expected.

当你尝试了这一点要小心,因为这可能会破坏其它功能。我不是很舒服,在这样一个漫长的源$ C ​​$ C黑客。如果有人谁是一个实际的NG-网格团队成员看到这一点,他能告诉我,如果我发现了一个bug,以及如何举报。 (从来没有这样做之前!)

Be carefull when you try this out, because it may break other functionality. I'm not very comfortable with hacking in such a lengthy source code. If someone who is an actual ng-grid team member sees this he can tell me if I found a bug and how to report it. (Never done that before!)

这篇关于NG-格:为什么afterSelectionChange()调用两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆