Skip to content

Instantly share code, notes, and snippets.

@SamDuvall
Last active Aug 29, 2015
Embed
What would you like to do?
View State in Angular 1.x
var states = {
'users': {
url: '/users',
templateUrl: 'users.html',
controller: 'usersCtrl',
resolve: {
groups: function(Group) {
return Group.query().$promise;
},
users: function(User) {
return User.query().$promise;
}
}
}
}
<table>
<thead>
<tr>
<td>Email</td>
<td>Full Name</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="user in users | users:{group: group}">
<td>{{user.email}}</td>
<td>{{user.fullName}}</td>
</tr>
</tbody>
</table>
app.filter('users', function() {
return function(users, filter) {
var filters = [];
// Group filter?
if (filter.group) filters.push(function(user) {
return _.findWhere(filter.group.members, {id: user.id})
});
// Run the filter on each user
if (!filters.length) return users;
return _.filter(users, function(user) {
return _.all(filters, function(filter) { return filter(user) });
});
}
});
app.controller('usersCtrl', function($scope, $location, users) {
// Watch for state changes that we care about
$scope.$watch(function() {
var search = $location.search();
if (search.group) return parseInt(search.group, 10);
}, function(groupId) {
$scope.group = _.findWhere($scope.groups, {id: groupId});
});
// User changed the group filter (allow empty filter)
$scope.setGroup = function(group) {
if (group) var groupId = group.id;
$location.search('group', groupId);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment