NG-重复排序jQuery中抛出异常 [英] ng-repeat sorting is throwing an exception in jQuery

查看:166
本文介绍了NG-重复排序jQuery中抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与吴重复创建行的表。表头有一个NG单击设置predicate的排序(该功能还确定方向,递增/递减)。排序工作正常,但由于某种原因,我得到异常的jQuery的每一次我改变predicate和排序火灾。

I have a table with rows created by ng-repeat. Table headers have an ng-click that sets the predicate for the sorting (the function also determines direction, asc/desc). The sorting works fine, but for some reason I get exceptions from jQuery every time I change the predicate and the sort fires.

下面是我在做什么的plunkr例如:的http:// plnkr。 CO /编辑/ qfNcm9RPQSsNgqmm3TYS?p = preVIEW

Here is a plunkr example of what I am doing : http://plnkr.co/edit/qfNcm9RPQSsNgqmm3TYS?p=preview

正如你可以在plnkr看到,NG-重复是pretty简单。吴重复我们的项目是类似的,没有什么更复杂的。

As you can see in the plnkr, the ng-repeat is pretty simple. The ng-repeat in our project is similar and not any more complicated.

<tr ng-repeat="contest in AllContests | orderBy:sort:ReverseSort" ng-show="contest.isVisible">

这实际发生在许多我们code地点和我们使用其中排序的方法不同,但它们仍受到影响。

This actually happens in a number of places in our code and we use different methods of sorting among them, but they are still affected.

表头可点击的项目进行排序。该plunkr不会出现问题。即使在我的网页排序的作品,它打破了页面上的其他东西(如华而不实的旋转木马控制 )。唯一的例外是在(通过谷歌CDN包括Sizzle.js版本2.1.1)的jquery.js线1430。在其中发生异常的方法是Sizzle.attr。它看起来像正在发生的事情是它是通过所有的NG-重复创建的所有元素的循环,并从中获取属性值。当它获取异常发生的结束ngRepeat:在AllContests大赛|排序依据:排序:ReverseSort这标志着NG重复部分的结尾评论

The table headers can be clicked to sort the items. That plunkr does not exhibit the problem. Even though the sorting works on my page, it breaks other things on the page (like a slick carousel control). The exception is on line 1430 in jquery.js (ver 2.1.1 via Google CDN which includes Sizzle.js). The method in which the exception happens is Sizzle.attr. It looks like what is happening is it is looping through all of the elements created with the ng-repeat, and getting attribute values from them. The exception happens when it gets to the " end ngRepeat: contest in AllContests | orderBy:sort:ReverseSort " comment that signifies the end of the ng-repeat section.

具体我得到。未捕获类型错误:未定义是不是一个函数时,它会尝试的getAttribute()元素的

Specifically I am getting 'Uncaught TypeError: undefined is not a function' when it tries to getAttribute() on the element.

return val !== undefined ?
    val :
    support.attributes || !documentIsHTML ?
        elem.getAttribute( name ) :
        (val = elem.getAttributeNode(name)) && val.specified ?
            val.value :
            null;

现在,我可以拉了jQuery,以便它评估elem.nodeName ==#COMMENT添加到我的项目编辑,只是返回null在这种情况下。我宁愿知道如果我正确做事或是否有这样做的更好的方法。

Now, I could pull jquery, add it to my project edit it so that it evaluates elem.nodeName == "#comment", and just return null in that case. I would much rather know if I am doing something incorrectly or if there is a better way of doing this.

另外,有压倒一切的方法并提供我自己不编辑jQuery库的一种方式?

Otherwise, is there a way of overriding that method and supplying my own without editing the jquery library?

下面是在行动问题的一个例子:
https://playmlf.com/Lobby/ContestLobby

Here is an example of the problem in action: https://playmlf.com/Lobby/ContestLobby

我已经编辑了jQuery库,使它看起来正是如此:

I have edited the jQuery library so that it looks thusly:

Sizzle.attr = function (elem, name) {
    if (elem.nodeName == '#comment') return null;
    // Set document vars if needed
    if ( ( elem.ownerDocument || elem ) !== document ) {
        setDocument( elem );
    }

请注意:这只是Sizzle.attr方法的顶部。第一个如果是感兴趣的行。如果元素是一个评论它返回null。我不想编辑jQuery库,但我无法弄清楚如何重写此特定的方法,或者把它从排序过程中被称为摆在首位。我不喜欢这个功能,但在同一时间,我花了在这条路上太多的时间和需要的一些作品。我将在后面付出了困境税。

NOTE: this is only the top portion of the Sizzle.attr method. The first if is the line of interest. It returns null if the element is a comment. I did not want to edit the jquery library, but I was unable to figure out how to override this particular method, or keep it from being called in the first place during a sort. I don't like this fix, but at the same time I have spent WAY too much time on this and need something that works. I will pay the bad fix tax later.

推荐答案

这似乎发生,因为注释元素没有getAttribute方法。

This seems to occur because Comment element does not have getAttribute method.

href=\"https://github.com/jquery/sizzle/issues/309\">记录这个错误。作为一种变通方法,您可以添加到您的codeBase的,并确保其得到执行初期:

There is a bug logged for this. As a workaround, you can add this to your codebase and make sure that it gets executed early on:

Object.getPrototypeOf(document.createComment('')).getAttribute = function() {}

这篇关于NG-重复排序jQuery中抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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