Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. <body ng-app="app">
  2.  
  3. <div ng-controller="mainCtrl">
  4. <div list-directive />
  5. </div>
  6.  
  7. <script type="text/ng-template" id="list.html">
  8. <input type="checkbox" class="control">
  9.  
  10. <div ng-repeat="player in model">
  11. Name: {{player.firstName}}, Price: {{player.price}}
  12. </div>
  13. </script>
  14.  
  15. var app = angular.module('app', []);
  16.  
  17. app.controller('mainCtrl', ['$scope', '$filter', function($scope, $filter){
  18. $scope.model = [{ firstName: 'foo', price: 100 }, { firstName: 'bar', price: 50 }, { firstName: 'foobar', price: 0}];
  19.  
  20. $scope.filter = function() {
  21. $scope.model = $filter('listFilter')($scope.model);
  22. console.log($scope.model);
  23. }
  24. }]);
  25.  
  26. app.directive('listDirective', function(){
  27. return {
  28. restrict: 'AE',
  29. templateUrl: 'list.html',
  30. link: function($scope, iElm, iAttrs, controller) {
  31. iElm.bind('click', function(e){
  32. var el = angular.element(e.target);
  33.  
  34. if (el.hasClass('control')) {
  35. $scope.filter();
  36. };
  37. });
  38. }
  39. };
  40. });
  41.  
  42. app.filter('listFilter', function(){
  43. return function(input) {
  44. var results = [];
  45.  
  46. angular.forEach(input, function(val, key){
  47. if (val.price != 0) {
  48. results.push(val);
  49. }
  50. });
  51.  
  52. return results;
  53. }
  54. });
  55.  
  56. $scope.filter = function() {
  57. $scope.model = $filter('listFilter')($scope.model);
  58. $scope.$apply();
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement