Monday 6 June 2016

Create a Reusable Login Directive or LoginTag in HTML5 using Angular js

In this post we are going to see how to create a reusable login directive or login tag in HTML5 using angular js.

Every time when we create the web application we have some things are do repeatedly , creating login page, register page etc. so to render the Login form now we are going to create a Login Tag which can be used in any web application.

<div class="" style="margin-top:10px">
    <div>
     <login-form action="/Validation/Login"></login-form>
  </div>

</div>

Like above we have to use the login-form tag in any web application to render a Login form like below.






Login directive in angular js



var mainApp = angular.module('mainApp', []);

mainApp.directive('loginForm'function () {
    return {
        template: '<div class="card card-container">'+      
            '<img id="profile-img" class="profile-img-card"  
                       src="../../Content/avatar.png" />'+
            '<p id="profile-name" class="profile-name-card"></p>'+
            '<form method="post" action="{{action}}" validate-invalid 
                        class="form-signin" id="loginform">' +
                '<span id="reauth-email" class="reauth-email"></span>'+
                 '<input type="email" name="UserName" ng-model="user.email" 
                         id="inputEmail" 
                         class="form-control" placeholder="Email address" required>' +
                 '<input type="password" name="Password" 
                         ng-model="user.password" 
                         id="inputPassword" class="form-control" 
                         placeholder="Password" required>' +
                 '<div id="remember" class="checkbox">'+
                    '<label><input name="RememberMe" type="checkbox" ng-model="user.remember"                            value="remember-me"> Remember me</label>' +
                '</div>'+
                '<button ng-disabled="!form.enableSubmit" 
                         class="btn btn-lg btn-primary btn-block btn-signin" 
                         type="submit" >Sign in</button>' +
            '</form>'+
            '<a href="#" class="forgot-password" 
                         style="color:rgb(104, 145, 162);">Forgot the password?</a>'+
        '</div>',
        scope: {
            action:"@"
        },
        restrict: "E",
        link: function (scope,element,attr,cntrl) {
        }
    }

});

For the login-form we have to give the Post action method to redirect to post url for validation.


CSS:
:::::::::

.card-container.card {
    width350px;
    padding40px 40px;
}

.btn {
    font-weight700;
    height36px;
    -moz-user-selectnone;
    -webkit-user-selectnone;
    user-selectnone;
    cursordefault;
}

/*
 * Card component
 */
.card {
    background-color#F7F7F7;
    /* just in case there no content*/
    padding20px 25px 30px;
    margin0 auto 25px;
    margin-top50px;
    /* shadows and rounded borders */
    -moz-border-radius2px;
    -webkit-border-radius2px;
    border-radius2px;
    -moz-box-shadow0px 2px 2px rgba(0, 0, 0, 0.3);
    -webkit-box-shadow0px 2px 2px rgba(0, 0, 0, 0.3);
    box-shadow0px 2px 2px rgba(0, 0, 0, 0.3);
}

.profile-img-card {
    width96px;
    height96px;
    margin0 auto 10px;
    displayblock;
    -moz-border-radius50%;
    -webkit-border-radius50%;
    border-radius50%;
}

/*
 * Form styles
 */
.profile-name-card {
    font-size16px;
    font-weightbold;
    text-aligncenter;
    margin10px 0 0;
    min-height1em;
}

.reauth-email {
    displayblock;
    color#404040;
    line-height2;
    margin-bottom10px;
    font-size14px;
    text-aligncenter;
    overflowhidden;
    text-overflowellipsis;
    white-spacenowrap;
    -moz-box-sizingborder-box;
    -webkit-box-sizingborder-box;
    box-sizingborder-box;
}

.form-signin #inputEmail,
.form-signin #inputPassword {
    directionltr;
    height44px;
    font-size16px;
}

.form-signin input[type=email],
.form-signin input[type=password],
.form-signin input[type=text],
.form-signin button {
    width100%;
    displayblock;
    margin-bottom10px;
    z-index1;
    positionrelative;
    -moz-box-sizingborder-box;
    -webkit-box-sizingborder-box;
    box-sizingborder-box;
}

.form-signin .form-control:focus {
    border-colorrgb(104, 145, 162);
    outline0;
    -webkit-box-shadowinset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgb(104, 145, 162);
    box-shadowinset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgb(104, 145, 162);
}

.btn-signin {
    /*background-color: #4d90fe; */
    background-colorrgb(104, 145, 162);
    /* background-color: linear-gradient(rgb(104, 145, 162), rgb(12, 97, 33));*/
    padding0px;
    font-weight700;
    font-size14px;
    height36px;
    -moz-border-radius3px;
    -webkit-border-radius3px;
    border-radius3px;
    bordernone;
    -o-transitionall 0.218s;
    -moz-transitionall 0.218s;
    -webkit-transitionall 0.218s;
    transitionall 0.218s;
}

.btn-signin:hover,
.btn-signin:active,
.btn-signin:focus {
    background-colorrgb(12, 97, 33);
}

.forgot-password {
    colorrgb(104, 145, 162);
}

.forgot-password:hover,
.forgot-password:active,
.forgot-password:focus{
    colorrgb(12, 97, 33);
}

button:disabled{

    background-color:gray !important;



For Check the form submit button invalid:

mainApp.directive('validateInvalid'function () {
    return {
        restrict: "A",
        link: function (scope, element, attrs) {

            if (scope.form == undefined)
                scope.form = {};

            element.addClass('ng-invalid');

            scope.form.enableSubmit = false;

            scope.$watch(function () {
                return element.attr('class');
            }, function (val) {
                var valid = true;
                var classes = val.split(" ");

                angular.forEach(classes, function (classname) {
                    if (classname == "ng-invalid") {
                        valid = false;
                    }                   
                });

                if (valid)
                    scope.form.enableSubmit = true;
                else
                    scope.form.enableSubmit = false;

            });
        }
    }

});

Output

OnLoad





After Validation



After Successful Login





From this Post you can learn how to create a reusable Lgin-form tag and use in many web application development and reduce time of developing the applications.

No comments:

Post a Comment