Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error 'orderBy' when updating to Angular 1.5.3

I updated Angular on my project, from 1.4.9 to 1.5.3. And on one of the pages I'm getting this error message:

'Error: orderBy:notarray Value is not array-like', 'Expected array but received: 0'

Here is template:

<tr ng-repeat="targeting in vm.TargetingsAudience track by $index  | orderBy:orderByName">
                    <td>
                        {{targeting.Name}}
                    </td>
                    <td class="au_content_descr">
                        <p ng-repeat="val in targeting.Values track by $index  | orderBy:orderByName" class="targeting-value">{{val}}</p>
                    </td>
                    <td class="au_ico_2">
                        <a class="au_del au_fast_ico" ng-click="vm.removeTargeting(targeting)"><i class="glyphicon glyphicon-remove"></i></a>
                        <a class="au_edit au_fast_ico" ng-click="vm.editTargeting(targeting)"><i class="glyphicon glyphicon-pencil"></i></a>
                    </td>
                </tr>

vm.TargetingsAudience - is an Array of Objects:

[{Name: "Гео", TargetingCategory: "Audience", TypeId:"Location", Values: [0: "Россия", 1: "Москва", 2: "Московская область"]}]
like image 932
Максим Лебидь Avatar asked Apr 06 '16 10:04

Максим Лебидь


People also ask

What is orderBy in angular?

An orderBy Filter in AngularJS is used to sort the given array to the specific order. The default order of sorting the string is in alphabetical order whereas the numbers are numerically sorted. By default, all the items are sorted in ascending order, if the ordering sequence is not specified.

How to use sorting in AngularJS?

To sort in descending order, set it as true . You can also use + to sort the data in an ascending and – the data in descending order also . Here with the filters in Angular JS, instead of displaying the various rows, we will be sorting it by ascending and descending order .

What is track by $Index in AngularJS?

Track by $index in AngularJS The ngRepeat track by $index allows you to specify which items should be repeated using their index number. The ngRepeat is a directive that can be added to the HTML template of an Angular application. It is used for creating lists, and it can take an expression as an argument.


2 Answers

This might be related to a breaking change in angular 1.5

Filters (orderBy)

Due to 2a85a634, passing a non-array-like value (other than undefined or null) through the orderBy filter will throw an error. Previously, the input was returned unchanged, which could lead to hard-to-spot bugs and was not consistent with other filters (e.g. filter). Objects considered array-like include: arrays, array subclasses, strings, NodeLists, jqLite/jQuery collections

Try using AngularJS toArray Filter

EDIT :

Because you sad you upgraded the app, I assumed it was working before. But to make it working I think you have to switch track by and order by statements

Note: track by must always be the last expression:

<tr ng-repeat="targeting in vm.TargetingsAudience | orderBy:orderByName track by $index  ">

<p ng-repeat="val in targeting.Values | orderBy:orderByName  track by $index  " class="targeting-value">{{val}}</p>
like image 200
jowey Avatar answered Oct 05 '22 02:10

jowey


Change orderBy:orderByName to orderBy:'Name'

like image 27
Alex Logan Avatar answered Oct 05 '22 03:10

Alex Logan