Reputation: 5451
I'd like to init a filtered list with angularjs, I don't know how to access to ng-init variables...
Edit #1 :
app.controller("AgreementsController", function($scope, $http, $filter) {
$scope.agreements = [];
$http.get('/api/agreement').success(function(data, status, headers, config) {
$scope.agreements = data.agreements;
$scope.filteredAgreements = $filter('filter')($scope.agreements,
{number: $scope.search});
});
<tbody ng-init="filteredAgreements = (agreements | filter:{number:search})">
<tr ng-repeat="agreement in agreements | filter:{number:search} | limitTo:5">
<td>{{agreement.number}}</td>
</tr>
</tbody>
{{filteredAgreements.length}} <!-- 291 even if I put "65" into the search -->
Upvotes: 0
Views: 975
Reputation: 26838
You get agreements
via AJAX, that means at the time the ngInit
directive runs they are empty and therefore filteredAgreements
will be empty, too. You have to wait until the AJAX call returns.
To use an angular filter in JavaScript you need the $filter
service. You call it with the name of the filter to get the filter, and then call that function with the data to be filtered (in your case agreements
) and the desired arguments.
app.controller("AgreementsController", function($scope, $http, $filter) {
$scope.agreements = [];
$http.get('/api/agreement').success(function(data, status, headers, config) {
$scope.agreements = data.agreements;
$scope.filteredAgreements = $filter('filter')($scope.agreements,
{number: $scope.search});
});
References:
Upvotes: 1