简体   繁体   中英

AngularJS 1.2.21 forEach is not compatible with ng-grid?

Is there a bug related to forEach in AngularJS 1.2.21 that was not in AngularJS 1.2.20 ?

I tested the following code (when the ng-grid is loaded):

var contor = 0;
$scope.$watch('gridOptions.$gridScope.columns', functionForColumnsChange, true);
...
function functionForColumnsChange(newv, oldv) {
    if ( newv !== oldv ) {
        console.log(contor++);
    }
}

If I use:

https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.js

the output is

0

but if I use

https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js

the output is

TypeError: Cannot set property 'sortPriority' of undefined at http://angular-ui.github.io/ng-grid/lib/ng-grid.js:1668:36 at Object.forEach ( https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js:325:18 ) at self.sortData ( http://angular-ui.github.io/ng-grid/lib/ng-grid.js:1667:25 ) at self.sortColumnsInit ( http://angular-ui.github.io/ng-grid/lib/ng-grid.js:1701:18 ) at Object.ngGridDirectives.directive.ngGridDirective.compile.pre.grid.init.then.dataWatcher [as fn] ( http://angular-ui.github.io/ng-grid/lib/ng-grid.js:2943:42 ) at Scope.$get.Scope.$digest ( https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js:12471:29 ) at Scope.$get.Scope.$apply ( https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js:12736:24 ) at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js:1438:15 at Object.invoke ( https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js:3940:17 ) at doBootstrap ( https://ajax.googleapis.com/ajax/libs/angularjs/1.2. 21/angular.js:1436:14 )

0

1

Why that error ? And why 1 after 0 ?

If I change the ng-grid.js commenting 2 lines:

//c.sortPriority = i + 1;
//push(c);

the error message is not there anymore, but there is "1".

The ng-grid that is used is from this source: http://angular-ui.github.io/ng-grid/lib/ng-grid.js (there is the same problem for the minified version).


Later edit: The problem seems to be related to this issue: https://github.com/angular/angular.js/commit/36625de0d3ebc1fc091af474d942c6ce16b0a1c0

The difference is only in Google Chrome and Mozilla Firefox .

In IE8 0 and 1 are printed for both versions of AngularJS. Output in IE8:

TypeError: 'undefined' is null or not an object

0

1

The problem was that I had a wrong $scope.sortInfo array:

$scope.sortInfo = {fields: ["N", "A", "B", "C"], directions: ['asc']};

even if "N" is not a column at this moment.

I found 3 possible solutions:

1) Change the array :

$scope.sortInfo = {fields: ["A", "B", "C"], directions: ['asc']};

2) Change the ng-grid to be more permissive:

                if ( typeof c !== 'undefined' ) {
                    c.sortPriority = i + 1;
                    push(c);
                }

3) Change the AngularJS to be more permissive:

for (key = 0; key < obj.length; key++) {
    if ( typeof obj[key] !== 'undefined' ) {
        iterator.call(context, obj[key], key);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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