Reputation: 35
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);
Upvotes: 1
Views: 512
Reputation: 123739
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');
Upvotes: 3