Having issues with getting this filter to work.
$scope.imgCollection.then(function (images) {
$scope.images = images.thisGal_images;
if ($scope.images[0].order == '0') {
console.log('orgName');
$scope.images = $filter('orderBy')($scope.images, 'orgName');
} else {
console.log('sort order');
$scope.images = $filter('orderBy')($scope.images, 'sortOrder');
console.log($scope.images);
}
});
$scope.images returns a list of images from the database. On the initial upload, the sortOrder column is populated with '0' as they can be sorted via ui:sortable. So on the initial view I base the sort order on the file name. After the initial view the DB is written and the first image is given the sortOrder of 1 and increments from there.
This could be my misunderstanding of $filter, but $scope.images = $filter('orderBy')($scope.images,'sortOrder');
is not ordering my $scope.images based on sortOrder.
Thanks
Overview. Filters are used for formatting data displayed to the user. They can be used in view templates, controllers or services. AngularJS comes with a collection of built-in filters, but it is easy to define your own as well.
In AngularJS, you can also inject the $filter service within the controller and can use it with the following syntax for the filter. Syntax: $filter("filter")(array, expression, compare, propertyKey) function myCtrl($scope, $filter) { $scope. finalResult = $filter("filter")( $scope.
Answer: A is the correct option. The syntax of applying multiple filters in AngularJS can be written as: {{ expression | filter1 | filter2 | ... }}
I created a demo for you and hopefully you can compare the code and figure out the issue.
I guess you might forget to inject $filter
module. Please take a look a the demo.
<div ng-app="myApp" ng-controller="ctrl">
<div ng-repeat="image in images">{{image}}</div>
<button ng-click="order('0')">By orgName</button>
<button ng-click="order('1')">By sortOrder</button>
</div>
var app = angular.module('myApp', []);
function ctrl($scope, $filter) {
$scope.images = [{
orgName: 'B',
sortOrder: 111
}, {
orgName: 'A',
sortOrder: 12
}, {
orgName: 'D',
sortOrder: 13
}, {
orgName: 'C',
sortOrder: 14
}];
$scope.order = function (order) {
if (order == '0') {
$scope.images = $filter('orderBy')($scope.images, 'orgName');
} else {
$scope.images = $filter('orderBy')($scope.images, 'sortOrder');
}
}
}
Working Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With