Last active
August 29, 2015 14:01
-
-
Save mrded/d9acd9e2faf35bdbb095 to your computer and use it in GitHub Desktop.
AngularJS: Example of filtering problem.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.controller('GlobalCtrl', function($rootScope, $scope, UserService, TodoService) { | |
$rootScope.users = []; | |
$rootScope.todos = []; | |
// Init. | |
UserService.all().then(function(users) { | |
angular.forEach(users, function(user) { | |
rootScope.users.push(user); | |
TodoService.all(user.id).then(function(todos) { | |
angular.forEach(todos, function(todo) { | |
$rootScope.todos.push(todo); | |
}); | |
$rootScope.$broadcast('UPDATE_DATA', '') | |
}); | |
}); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div ng-controller="GlobalCtrl"> | |
<div ng-repeat="user in users" ng-controller="UserCtrl"> | |
<ul> | |
<!-- todos must be variable --> | |
<li ng-repeat="todo in todos">{{todo.title}}</li> | |
</ul> | |
</div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.service('TodoService', function($http, $q) { | |
this.all = function(userId) { | |
var deferred = $q.defer(); | |
$http.get('/api/users/' + userId + '/todos').success(function(todos) { | |
return deferred.resolve(todos); | |
}); | |
return deferred.promise; | |
}; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.controller('UserCtrl', function($rootScope, $scope, $filter) { | |
$rootScope.$on('UPDATE_DATA', function(event, data) { | |
$scope.todos = $filter('filter')($rootScope.todos, {user_id: $scope.user.id}); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.service('UserService', function($http, $q) { | |
this.all = function() { | |
var deferred = $q.defer(); | |
$http.get('/api/users').success(function(users) { | |
return deferred.resolve(users); | |
}); | |
return deferred.promise; | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment