diff --git a/docs/content/error/orderBy/notarray.ngdoc b/docs/content/error/orderBy/notarray.ngdoc new file mode 100644 index 000000000000..87845d067a74 --- /dev/null +++ b/docs/content/error/orderBy/notarray.ngdoc @@ -0,0 +1,50 @@ +@ngdoc error +@name orderBy:notarray +@fullName Value is not array-like +@description + +This error occurs when {@link ng.orderBy orderBy} is not passed an array-like value: +```html + +
+ {{ key }} : {{ value }} +
+``` + +orderBy must be used with an array-like value so a subset of items can be returned. +The array can be initialized asynchronously and therefore `null` or `undefined` won't throw this error. + +To use orderBy to order the properties of an object, you can create your own array based on that object: +```js +angular.module('aModule', []) + .controller('aController', function($scope) { + var myObj = { + one: {id: 1, name: 'Some thing'}, + two: {id: 2, name: 'Another thing'}, + three: {id: 3, name: 'A third thing'} + }; + + $scope.arrFromMyObj = Object.keys(myObj).map(function(key) { + return myObj[key]; + }); + }); +``` +That can be used as: +```html + +
+ {{ key }} : {{ value }} +
+``` + +You could as well convert the object to an array using a filter such as +[toArrayFilter](https://github.com/petebacondarwin/angular-toArrayFilter): +```html + +
+ {{ item }} +
+``` diff --git a/src/ng/filter/orderBy.js b/src/ng/filter/orderBy.js index 2e9e946d0c75..f6095c50106b 100644 --- a/src/ng/filter/orderBy.js +++ b/src/ng/filter/orderBy.js @@ -177,7 +177,9 @@ orderByFilter.$inject = ['$parse']; function orderByFilter($parse) { return function(array, sortPredicate, reverseOrder) { - if (!(isArrayLike(array))) return array; + if (!array) { return array; } + + if (!isArrayLike(array)) throw minErr('orderBy')('notarray', 'Expected array but received: {0}', array); if (!isArray(sortPredicate)) { sortPredicate = [sortPredicate]; } if (sortPredicate.length === 0) { sortPredicate = ['+']; } diff --git a/test/ng/directive/ngOptionsSpec.js b/test/ng/directive/ngOptionsSpec.js index c5dac4b72914..1634811a22df 100644 --- a/test/ng/directive/ngOptionsSpec.js +++ b/test/ng/directive/ngOptionsSpec.js @@ -125,7 +125,7 @@ describe('ngOptions', function() { .directive('oCompileContents', function() { return { - link: function(scope, element) { + link: function(scope, element) { linkLog.push('linkCompileContents'); $compile(element.contents())(scope); } diff --git a/test/ng/filter/orderBySpec.js b/test/ng/filter/orderBySpec.js index 4f371ba80aca..131e98d362e8 100644 --- a/test/ng/filter/orderBySpec.js +++ b/test/ng/filter/orderBySpec.js @@ -8,6 +8,20 @@ describe('Filter: orderBy', function() { describe('(Arrays)', function() { + it('should throw an exception if no array-like object is provided', function() { + expect(function() { orderBy({}); }). + toThrowMinErr('orderBy', 'notarray', 'Expected array but received: {}'); + }); + + it('should not throw an exception if a null or undefined value is provided', function() { + expect(orderBy(null)).toEqual(null); + expect(orderBy(undefined)).toEqual(undefined); + }); + + it('should not throw an exception if an array-like object is provided', function() { + expect(orderBy('cba')).toEqual(['a', 'b', 'c']); + }); + it('should return sorted array if predicate is not provided', function() { expect(orderBy([2, 1, 3])).toEqual([1, 2, 3]);