Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <body ng-app="app">
- <div ng-controller="mainCtrl">
- <div list-directive />
- </div>
- <script type="text/ng-template" id="list.html">
- <input type="checkbox" class="control">
- <div ng-repeat="player in model">
- Name: {{player.firstName}}, Price: {{player.price}}
- </div>
- </script>
- var app = angular.module('app', []);
- app.controller('mainCtrl', ['$scope', '$filter', function($scope, $filter){
- $scope.model = [{ firstName: 'foo', price: 100 }, { firstName: 'bar', price: 50 }, { firstName: 'foobar', price: 0}];
- $scope.filter = function() {
- $scope.model = $filter('listFilter')($scope.model);
- console.log($scope.model);
- }
- }]);
- app.directive('listDirective', function(){
- return {
- restrict: 'AE',
- templateUrl: 'list.html',
- link: function($scope, iElm, iAttrs, controller) {
- iElm.bind('click', function(e){
- var el = angular.element(e.target);
- if (el.hasClass('control')) {
- $scope.filter();
- };
- });
- }
- };
- });
- app.filter('listFilter', function(){
- return function(input) {
- var results = [];
- angular.forEach(input, function(val, key){
- if (val.price != 0) {
- results.push(val);
- }
- });
- return results;
- }
- });
- $scope.filter = function() {
- $scope.model = $filter('listFilter')($scope.model);
- $scope.$apply();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement