Introduction
This article demonstrates MVC using Angular UI calendar with JSON file in Visual Studio 2017.
Following the steps below you can use Angular UI calendar in Angular JS in MVC
- Create MVC Project
- Configure Angular UI calendar
- Work in Angular UI calendar
Create MVC Project
Open Visual Studio 2017
Go to New menu >Click New & project. Now it will open New Project Window
You can select ASP.NET Web Application on Framework 4.5. Enter the name of project in “Solution name” textbox then click ok button.
One more Window should be appearing. Select MVC Template in this popup & Click ok button. Now start cropping image.
Configure Angular UI Calendar
You can download the plug in from
Open the _Layout.cshtml and must refer the .js file from downloaded folder to this view page
- <script src="~/Plugin/angular/angular.min.js"></script>
- <script src="~/Plugin/angular-ui-router/release/angular-ui-router.min.js"></script>
- <script src="~/Plugin/moment/min/moment-with-locales.min.js"></script>
- <script src="~/Plugin/fullcalendar/dist/fullcalendar.min.js"></script>
- <link href="~/Plugin/fullcalendar/dist/fullcalendar.min.css" rel="stylesheet" />
- <script src="~/Plugin/angular-ui-calendar/src/calendar.js"></script>
- <script src="~/Plugin/fullcalendar/dist/gcal.js"></script>
Link your angular configurable file, whatever you given name
- <script src="~/App/App.module.js"></script>
- <script src="~/App/App.config.js"></script>
- <script src="~/App/CalController.js"></script>
Angular Module
You will need to include the module as a dependency on your application.
- var uiroute = angular
- .module('uiroute', ['ui.router','ui.calendar']);
If you have any doubt about configuration, visit the following articles -
Work in Angular UI Calendar
You can set as Angular UI with the ui-calendar directive in the html
- div ng-model="eventSources"
- calendar="myCalendar"
- ui-calendar="uiConfig.calendar" class="calendar"></div>
Html Code
- <div ng-controller="CalendarController" >
- <div class="panel panel-default">
- <div class="panel-heading">
- <div class="row">
-
- <div class="col-md-6">
- <div class="btn-switch mb btn-switch-purple">
- <button ng-click="toggleEventSource()" > Load Json</button>
- </div>
- </div>
- </div>
- </div>
- <div class="panel-body">
- <div ng-model="eventSources"
- calendar="myCalendar"
- ui-calendar="uiConfig.calendar" class="calendar"></div>
- </div>
-
- </div>
- </div>
Angular Controller
Simply initiate the events for calendar
- $scope.today = new Date();
- var date = new Date();
- var d = date.getDate();
- var m = date.getMonth();
- var y = date.getFullYear();
-
- $scope.calEventsPers = {
- id: 0,
- visible: true,
- className: ['fc-id-0'],
- events: [
- { id: 324, title: 'All Day Event', start: new Date(y, m, 1) },
- { title: 'Long Event', start: new Date(y, m, d - 5), end: new Date(y, m, d - 2) },
- { id: 999, title: 'Repeating Event', start: new Date(y, m, d - 3, 16, 0), allDay: false },
- { id: 999, title: 'Repeating Event', start: new Date(y, m, d + 4, 16, 0), allDay: false },
- { title: 'Birthday Party', start: new Date(y, m, d + 1, 19, 0), end: new Date(y, m, d + 1, 22, 30), allDay: false },
- { title: 'Click for Google', start: new Date(y, m, 28), end: new Date(y, m, 29), url: 'http://google.com/' }
- ]
- };
-
- $scope.eventSources = [$scope.calEventsPers];
uySet to the ui configuration, like the code given below
- $scope.uiConfig = {
- calendar: {
- height: 400,
- editable: true,
- header: {
- left: 'month,basicWeek,basicDay',
- center: 'title',
- right: 'prev,next today'
- },
- eventClick: $scope.alertOnEventClick,
- eventDrop: $scope.alertOnDrop,
- eventResize: $scope.alertOnResize,
-
- selectable: true,
- selectHelper: true,
- unselectAuto: true,
- select: function (start, end) {
- var title = prompt('Event Title:');
- var eventData;
- if (title) {
- eventData = {
- title: title,
- start: start.format(),
- end: end.format()
- };
- $scope.addEvent(eventData);
- }
- }
- } };
Click F5 button and Run the Application. Now it will appear in browser and you will see the result.
Output 1
Yes, we got the result. Let’s start binding the JSON file data. So create one json file in project explorer, write the code likes below.
JSON File
- [
-
- {
- "type": "party",
- "title": "Lunch ",
- "start": "01/01/2015",
- "end": "01/02/2015",
- "allDay": "false"
- },
- {
- "type": "link",
- "title": "google.com",
- "start": "01/01/2015",
- "end": "01/02/2015",
- "url": "http://google.com/"
- }
If you need to, you can bind the data from the database side with json format
Angular Controller
Retrieve the data in controller using $http service.
- $scope.toggleEventSource = function () {
- $http.get('server/calendar/external-calendar.json').success(function (data) {
-
- var calEventsExt = {
- id: 2,
- visible: true,
- color: 'green',
- textColor: '#fff',
- className: ['fc-id-2'],
- events: []
- };
-
-
-
- for (var i = 0; i < data.length; i++) {
- data[i].start = new Date(y, m, d + i, 12, 0);
- data[i].end = new Date(y, m, d + i, 14, 0);
- }
-
-
- calEventsExt.events = angular.copy(data);
-
- $scope.eventSources.push(calEventsExt);
-
- });
- };
Output 2
Data will load when you click load Json button.
Let’s see the event in this calendar
Angular Controller
-
- $scope.alertOnEventClick = function (event, allDay, jsEvent, view) {
- alert(event.title + ' was clicked ');
- };
-
- $scope.alertOnDrop = function (event, dayDelta, minuteDelta, allDay, revertFunc, jsEvent, ui, view) {
- alert('Event Droped to make dayDelta ' + dayDelta);
- };
-
- $scope.alertOnResize = function (event, dayDelta, minuteDelta, revertFunc, jsEvent, ui, view) {
- alert('Event Resized to make dayDelta ' + minuteDelta);
- };
-
-
- $scope.addEvent = function (newEvent) {
- if (newEvent) {
- $scope.calEventsPers.events.push(newEvent);
- }
- };
-
-
- $scope.remove = function (index) {
- $scope.calEventsPers.events.splice(index, 1);
- };
-
- $scope.changeView = function (view, calendar) {
- $scope.myCalendar.fullCalendar('changeView', view);
- };
-
- $scope.renderCalender = function (calendar) {
- $scope.myCalendar.fullCalendar('render');
- };
After adding this code in your controller just run the application
Output 3
If you click on the above data, you can add the new event.
Conclusion
In this article, we have learned MVC using Angular UI calendar with JSON files. If you have any queries, please tell me through the comments section, because your comments are very valuable.
Happy Coding!...