【问题标题】:Infinite loops caused by ng-repeat and search filter resultsng-repeat 和搜索过滤结果导致的无限循环
【发布时间】:2016-07-31 09:32:04
【问题描述】:

返回搜索结果后如下:

[
  {
    "id": 0,
    "name": "UserManagement",
    "parent": "modules",
    "grandpa": "non",
    "viewname": "User Management",
    "level": "module",
    "template": "UserManagement",
    "keywords": "user management users manager managing ",
    "description": "module UserManagement My goal is simple. It is a complete understanding of the universe, why it is as it is and why it exists at all . Stephen Hawking",
    "partitions": {

    }
},
{
    "id": 1,
    "name": "UsersRoles",
    "viewname": "Users Roles",
    "level": "partition",
    "parent": "UserManagement",
    "grandpa": "modules",
    "template": "UsersRoles",
    "keywords": "users roles accounts ",
    "description": "partition UsersRoles The black hole information paradox is a puzzle resulting from the combination of quantum mechanics and general relativity",
    "usecases": {

    }
},
{
    "id": 2,
    "name": "Login",
    "viewname": "Log in",
    "level": "usecase",
    "parent": "UsersRoles",
    "grandpa": "UserManagement",
    "template": "Login",
    "keywords": "log in",
    "description": " usecase login black hole information paradox"
},
{
    "id": 3,
    "name": "EditProfile",
    "viewname": "Edit Profile",
    "level": "usecase",
    "parent": "UsersRoles",
    "grandpa": "UserManagement",
    "template": "EditProfile",
    "keywords": "edit profile",
    "description": "usecase editprofile black hole information paradox"
},
{
    "id": 7,
    "name": "Accounting",
    "parent": "modules",
    "grandpa": "non",
    "viewname": "Accounting",
    "level": "module",
    "template": "Accounting",
    "keywords": "accounts",
    "description": "module accounting My goal is simple. It is a complete understanding of the universe, why it is as it is and why it exists at all . Stephen Hawking",
    "partitions": {                        
    }
},
{
    "id": 10,
    "name": "NewFiscalRecord",
    "viewname": "New Fiscal Record",
    "level": "usecase",
    "parent": "AccountsAndFiscalRecords",
    "grandpa": "Accounting",
    "template": "NewFiscalRecord",
    "keywords": "NewFiscalRecord",
    "description": "usecase new fiscal record"
},
{
    "id": 11,
    "name": "FiscalReports",
    "viewname": "Fiscal Reports",
    "level": "partition",
    "parent": "Accounting",
    "grandpa": "modules",
    "template": "FiscalReports",
    "keywords": "FiscalReports",
    "description": "partition Fiscal Reports",
    "usecases": {
    }
}

] 为了按“爷爷”和“父母”属性对结果进行分组,我使用了 lodash.js,如下所示:

var nest = function (seq, keys) {
        //console.log(keys.length)
        if (!keys.length)
            return seq;
        var first = keys[0];
        var rest = keys.slice(1);
        return _.mapValues(_.groupBy(seq, first), function (value) {
            return nest(value, rest)
        });
    };
var nested = nest(result, ['grandpa','parent']);            

并将“嵌套”返回到 ng-repeat 中的视图, 'nested' 是 'searchFor' 过滤器返回的内容:

<div ng-repeat="node in filteredModules = (modules | searchFor:searchString track by $index">    
    {{node.name}}    
</div>

但它会导致这个问题,我不知道为什么。

错误:[$rootScope:infdig] 达到了 10 个 $digest() 迭代。中止! 观察者在最后 5 次迭代中触发:[[{"msg":"fn:regularInterceptedExpression","newVal":119,"oldVal":115}],[{"msg":"fn:regularInterceptedExpression","newVal" :123,"oldVal":119}],[{"msg":"fn:regularInterceptedExpression","newVal":127,"oldVal":123}],[{"msg":"fn:regularInterceptedExpression"," newVal":131,"oldVal":127}],[{"msg":"fn:regularInterceptedExpression","newVal":135,"oldVal":131}]]

注意:当我将结果返回到视图而不对数据进行分组时,不会导致该问题。并且在控制台中打印“嵌套”而不在视图上查看时,它运行良好并且不会导致错误。 这里试图解释更多:https://jsfiddle.net/qw332bkc/

编辑:还有其他方法可以按“爷爷”和“父母”属性对结果进行分组吗?

【问题讨论】:

    标签: javascript arrays angularjs


    【解决方案1】:

    我也遇到过这种情况

    为我解决这个问题的方法是从 js 脚本文件中过滤,而不在 ng-repeat 标记中执行过滤操作

    因此,在您的情况下,解决方案可能是在控制器中创建一个过滤函数,该函数将在每次触发时为 $scope 分配一个排序数组。然后,从 ng-repeat 遍历这个数组。

    编辑:

    问题在于您每次都在创建一个新数组,因此 Angular 需要跟踪新的东西。据我所知, ng-repeat 运行,然后立即再次检查其集合以查看该循环中是否有任何变化。因为该函数返回一个新数组,这被认为是一个变化。

    取自: AngularJS InfDig error (infinite loop) with ng-repeat function that returns array of objects

    【讨论】:

    • 那么,您对发生的情况和导致错误有什么解释吗? @JRoller
    • @HalaElBarchah 请查看我的编辑以解释此错误的原因
    • 同意,尝试调试复杂的角度表达式是一场艰苦的战斗,在 javascript 中执行逻辑并尽可能简单的 ng-repeat 是一种更理智的方法
    猜你喜欢
    • 2014-04-19
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多