Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to do a simple array sort in a directive. But somehow when i put the nested array in the orderBy filter it is losing array and becomes an object (which can not be ordered)

when i log out the scope.item inside the directive it says: addresses: Array[2]

but when trying to filter using $filter('orderBy')(scope.item.addresses, 'distance'); i am getting "TypeError: object is not a function"

(function(angular){
'use strict';
angular.module('starter').directive('getDistance', function() {
    return {
        restrict: 'E',
        replace: true,
        link: function (scope, element, $filter) {

            ;
            console.log(scope.item);

            scope.item.addresses = $filter('orderBy')(, scope.item.addresses, 'distance');

            console.log(scope.item.addresses);
        }
    };
});
})(window.angular);
share|improve this question
    
You have an extra comma right after '$filter('orderBy')('. Typo? –  pixelbits 23 hours ago

1 Answer 1

up vote 2 down vote accepted

You need to inject filter in the directive definision not in the linking function.

angular.module('starter').directive('getDistance', function($filter) {

Or

angular.module('starter').directive('getDistance', ['$filter', function($filter) {..

and do (Removing the extra comma, which probably is a typo):

scope.item.addresses = $filter('orderBy')(scope.item.addresses, 'distance');
share|improve this answer
    
cheers, that did the job. Yes was indeed a typo here on SO –  Arjan 13 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.