I am trying to filter an ngRepeat by a specific array attribute.
js:
app.controller('UserIndexController', function ($scope, $http, $window) {
$scope.search = {};
$http({ method: 'GET', url: '/api/v2/group' })
.success(function(data, status, headers, config) {
$scope.groups = data;
})
.error(function(data, status, headers, config) {
});
$http({ method: 'GET', url: '/api/v2/user' })
.success(function(data, status, headers, config) {
$scope.users = data;
})
.error(function(data, status, headers, config) {
});
});
html:
<div data-ng-controller="UserIndexController">
<select
ng-model="search.group"
ng-options="group.name as group.name for group in groups"
class="form-control">
<option value="">Filter Users by Group</option>
</select>
<table class="table table-responsive table-striped table-hover">
<thead>
<tr>
<th>User Type</th>
<th>Email</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="user in users | filter:search.group">
<td><% user.groups[0].name %></td>
<td><% user.last_name %>, <% user.first_name %></td>
<td><% user.email %></td>
</tr>
</tbody>
</table>
</div>
The above code searches through the entire user object. This behaviour is not wanted because if any other user
data attributes match the string in the selected option, then this row will also be included in the filter.
How do I ensure that the filter only works on the user.groups[0].name
column?
<tr data-ng-repeat="user in users | filter:{user.groups[0].name: search.group}">
results in the following error: Syntax Error: Token '[' is unexpected
.
As such, I need something to either put user.groups[0].name
directly into the user object so that the filter doesn't have to access it via the array, or a way of filtering on the array object.