Skip to content

Instantly share code, notes, and snippets.

@anonymoussc
Created October 23, 2015 03:30
Show Gist options
  • Save anonymoussc/4acc62193d45273f68db to your computer and use it in GitHub Desktop.
Save anonymoussc/4acc62193d45273f68db to your computer and use it in GitHub Desktop.
Adding a Filter
<!DOCTYPE html>
<html ng-app="exampleApp">
<head>
<title>AngularJS Demo</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script>
var myApp = angular.module("exampleApp", []);
myApp.controller("dayCtrl", function($scope) {
$scope.day = new Date().getDay();
});
myApp.controller("tomorrowCtrl", function($scope) {
$scope.day = new Date().getDay() + 1;
});
myApp.directive("highlight", function() {
return function(scope, element, attrs) {
if (scope.day == attrs["highlight"]) {
element.css("color", "red");
}
}
});
myApp.filter("dayName", function() {
var dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
return function(input) {
return angular.isNumber(input) ? dayNames[input] : input;
};
});
</script>
</head>
<body>
<div class="panel">
<div class="page-header">
<h3>AngularJS App</h3>
</div>
<h4 ng-controller="dayCtrl" highlight="Monday">
Today is {{day || "(unknown)" | dayName}}
</h4>
<h4 ng-controller="tomorrowCtrl">
Tomorrow is {{day || "(unknown)" | dayName}}
</h4>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment