I'm still new to AngularJS, so I'm just trying to do a simple CRUD app. Currently I pull the data (in a JSON file) with $http
in a div
handled by a controller MyCtrl1
.
function MyCtrl1($scope, $http) { $http.get('data/accounts.json').success(function(data) { $scope.accounts = data; ... }); }
Inside this div
is a table
with the following tbody
:
<tbody> <tr ng-repeat="account in accounts | orderBy:sort.field:sort.desc | startFrom:currentPage * pageSize | limitTo:pageSize"> <td contentEditable="true" ng-repeat="(label, value) in account" ng-show="fields[label].visible">{{value}}</td> </tr> </tbody>
The orderBy
filter sorts according to a selected field; startFrom
slices the array to start at a certain point; limitTo
filters according to a preset page size. Without the pagination filters the performance was pretty terrible, but I was wonder if there was an alternative way to go about this?
I have Batarang for Chrome and under the Performance tab it showed ngRepeatWatch
taking up the most time, and I reckon it has to do with all the filtering I'm doing..
Answer: A is the correct option. The syntax of applying multiple filters in AngularJS can be written as: {{ expression | filter1 | filter2 | ... }}
You can consider using transclusion inside a custom directive, to achieve the behavior you are looking for without using ng-repeat.
The ng-repeat values can be filtered according to the ng-model in AngularJS by using the value of the input field as an expression in a filter. We can set the ng-model directive on an input field to filter ng-repeat values.
Directives that Create Scopes In most cases, directives and scopes interact but do not create new instances of scope. However, some directives, such as ng-controller and ng-repeat, create new child scopes and attach the child scope to the corresponding DOM element.
{{ expression | filter1 | filter2 }}
Just use
<tr ng-repeat="account in accounts | filter1 | filter2 | filter3" > <td contentEditable="true" ng-repeat="(label, value) in account" ng-show="fields[label].visible">{{value}}</td> </tr>
Angular 2 uses pipes, but its looks like filters:
<div>The chained hero's birthday is <p>{{ birthday | date:'fullDate' | uppercase}}</p> <div>
I know this question is old but for anyone in the future. Filtering in line is expensive (computationally) because it works directly on the DOM, if you are dealing with large amounts of data, 1000+ rows, it is much better to filter your collection in your controller then repeat on that instead.
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