Created
April 28, 2017 22:12
-
-
Save pcote/60bc839618cf561e1f0861826d201f6a to your computer and use it in GitHub Desktop.
An example on how to set up a full custom filter in angularjs in which the filter in question can take an argument.
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Custom Angular Filter Example</title> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> | |
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> | |
</head> | |
<body> | |
<div class="container" ng-app="app" ng-controller="controller as c"> | |
<div class="row"> | |
<div class="col-lg-4"> </div> | |
<div class="col-lg-4"> | |
<ul> | |
<li ng-repeat="num in c.numbers|modfilter:5">{{num}}</li> | |
</ul> | |
</div> | |
<div class="col-lg-4"> </div> | |
</div> | |
</div> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script> | |
<script> | |
var controller = function(){ | |
var c = this; | |
c.numbers = [1,2,3,4,5,6,7,8,9,10]; | |
}; | |
var modFilter = function(){ | |
var _myfilter = function(numbers, modVal){ | |
var newNumberList = []; | |
numbers.forEach(function(n){ | |
if(n % modVal == 0){ | |
newNumberList = newNumberList.concat(n); | |
} | |
}); | |
return newNumberList; | |
}; | |
return _myfilter; | |
}; | |
angular.module("app", []) | |
.controller("controller", controller) | |
.filter("modfilter", modFilter); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment