In ng-init directive, we call the getCurrentYear() function which will return the year from current date as:
$scope.getCurrentYear = function () {
return (new Date).getFullYear();
}
and assign to year variable and shown in code. Year will increase/decrease by 1 when we click on Next Year link/Previous Year link resp.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Welcome to LearnKode - A code learning platform</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>
<body ng-app="initExample">
<div ng-controller="ExampleController">
<div ng-init="year=getCurrentYear()">
<a href="" ng-click="year = year-1">Previous Year </a> | <a href="" ng-click="year = year+1">Next Year</a>
<div>Year: <b>{{ year }}</b></div>
</div>
</div>
<script>
var app = angular.module("initExample", []);
app.controller('ExampleController', ['$scope', function ($scope) {
$scope.getCurrentYear = function () {
return (new Date).getFullYear();
}
}]);
</script>
</body>
</html>