-
-
Save anonymoussc/351453ebfc96f653052a to your computer and use it in GitHub Desktop.
Creating a Simple Service
This file contains 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 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, days) { | |
$scope.day = days.today; | |
}); | |
myApp.controller("tomorrowCtrl", function($scope, days) { | |
$scope.day = days.tomorrow; | |
}); | |
myApp.directive("highlight", function($filter) { | |
var dayFilter = $filter("dayName"); | |
return function(scope, element, attrs) { | |
if (dayFilter(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; | |
}; | |
}); | |
myApp.service("days", function() { | |
this.today = new Date().getDay(); | |
this.tomorrow = this.today + 1; | |
}); | |
</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