Learn MVC Using Angular Idle

This article demonstrates MVC using Angular Idle with a UI Bootstrap. This article will show you how to get your application to report if a user is idle.

Angular Idle

Angular Idle can use an Angular module to detect and respond to idle users. We can almost maintain the session on the client side.

Follow the steps given below and we can use an Angular Idle in AngularJS in MVC.

  • Create MVC project.
  • Configure Angular Idle.
  • Work with Angular Idle.

Create an MVC Project

Open Visual Studio 2015.

Go to New menu >Click New and then Project. Now, it will open a New Project Window.

You can select ASP.NET Web Application on Framework 4.6. Enter the name of the project in the Solution name text box, then click OK.


One more Window should appear. Select the MVC template in this popup and click Ok. Now, you can start.

Configure Angular Idle

We will download the idle plugin from the following sources:

Open the _Layout.cshtml and 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/angular-idle/angular-idle.js"></script>
<script src="~/Plugin/angular-bootstrap/ui-bootstrap-tpls.min.js"></script>
<script src="~/App/App.module.js"></script>
<script src="~/App/App.config.js"></script>
<script src="~/App/CarController.js"></script> 
<script src="~/App/LoginConttroller.js"></script>

Angular Module

You will need to include the module as a dependency of your application.

var uiroute = angular
.module('uiroute', ['ui.router', 'ngIdle', 'ui.bootstrap'])

Angular Config

You should also set your options, using the KeepaliveProvider, IdleProvider in your Angular config file.

uiroute.config(['KeepaliveProvider', 'IdleProvider', function(KeepaliveProvider, IdleProvider) {

Angular Controller

You can set what action will be performed on the user idle times. With the below code, I will be shown a warning message and be notified of the session logout.

function closeModals() {
    if ($scope.warning) {
        $scope.warning = null;
    if ($scope.timedout) {
        $scope.timedout = null;
$scope.$on('IdleStart', function() {
    $scope.warning = $uibModal.open({
        templateUrl: 'warning-dialog.html',
        windowClass: 'modal-danger'
$scope.$on('IdleEnd', function() {
   // closeModals();
$scope.$on('IdleTimeout', function() {
    $scope.timedout = $uibModal.open({
        templateUrl: 'timedout-dialog.html',
        windowClass: 'modal-danger'

Angular Run

After configuring the angular.idle.js, you must initiate the function in your Angular Run function.

uiroute.run(['Idle', function(Idle) {


Set your HTML with the warning dialog and timeout dialog that is given below.

<script type="text/ng-template" id="warning-dialog.html">
    <div class="modal-header small danger">
        <h3>Your are Idle.</h3>
    <div idle-countdown="countdown" ng-init="countdown=5" class="modal-body">
        <uib-progressbar max="5" value="5" animate="false" class="progress-striped active">You'll be logged out in {{countdown}} second(s).</uib-progressbar>

<script type="text/ng-template" id="timedout-dialog.html">
    <div class="modal-header small warning">
        <h3>You have Timed Out!</h3>
    <div class="modal-body">
            You were idle too long. you'll be reset.
    <div class="modal-footer">
        <button class="btn btn-warning" ui-sref="login">Logout</button>

This code should configure your main Angular Controller and where we need to go to show this idle screen.

Angular Route

uiroute.config(function ($stateProvider, $urlRouterProvider){
        // State managing  
         .state('login', {
             url: '/login',
             templateUrl: '/App/Test/login.html',
             controller: 'LoginController'
        //Manager Role
        .state('manager', {
            url: '/manager',
            templateUrl: '/App/Manager/home.html'
         .state('manager.list', {
             url: '/list',
             templateUrl: '/App/Test/dataList.html',
             controller: 'CarController'

Run the Application. Now it will appear browser & see the result

Output 1


You need to refer to the login controller for login username and password.

if ($scope.UserName.toUpperCase() == 'MANAGER') {
    if ($scope.Password == '1') {

Output 2

After the Login Manager, the home page will appear, as shown below.


Output 3

Stay idle for 5 seconds. Now, click or touch the screen to reset your idle state.


Output 4

Over your idle minutes, it will ask you to Logout.


Output 5

Suppose a user is busy with some other tab in the browser. You can set an idle countdown and sessions expiration information will load on top of the browser tab.


In this article, we have taken a look at MVC using Angular Idle. If you have any queries, please tell me in the comments section. Your comments are very valuable.