AngularJS Toggle Password Visibility

This is the step by step procedure in this tutorial:

Step 1: Add the following dependency

In the header portion of your HTML, add the following:
  1. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  2. <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  3. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>

Step 2: Add Style

Include the following style.
  1. <style type="text/css">
  2.         .myInput{
  3.                 font-size: 1em;
  4.                 padding: 1em;
  5.         }
  6. </style>

Step 3: Add the input

Next, we create our input type.
  1. <input type="{{ inputType }}" class="myInput"> <button type="button" class="btn {{ btnType }}" ng-click="showhidePassword()"><i class="fa fa-{{ icon }}"></i> {{ btnName }}</button>   

Step 4: Creating our Angular JS Script

Last step is to create our script and name it as app.js.
  1. var app = angular.module('app', []);
  2.  
  3. app.controller('myCtrl', function($scope){
  4.         $scope.inputType = 'password';
  5.         $scope.btnName = 'Show';
  6.         $scope.btnType = 'btn-primary';
  7.         $scope.icon = 'eye';
  8.  
  9.         $scope.showhidePassword = function(){
  10.                 if ($scope.inputType == 'password'){
  11.                         $scope.inputType = 'text';
  12.                         $scope.btnName = 'Hide';
  13.                         $scope.btnType = 'btn-default';
  14.                         $scope.icon = 'eye-slash';
  15.                 }
  16.                 else{
  17.                         $scope.inputType = 'password';
  18.                         $scope.btnName = 'Show';
  19.                         $scope.btnType = 'btn-primary';
  20.                         $scope.icon = 'eye';
  21.                 }
  22.         }
  23. });

Full HTML

  1. <!DOCTYPE html>
  2. <html ng-app="app">
  3.         <title>AngularJS Toggle Password Visibility</title>
  4.         <meta charset="utf-8">
  5.         <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  6.         <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  7.         <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
  8.         <style type="text/css">
  9.                 .myInput{
  10.                         font-size: 1em;
  11.                         padding: 1em;
  12.                 }
  13.         </style>
  14. </head>
  15. <body ng-controller="myCtrl">
  16. <div class="container">
  17.         <h1 class="page-header text-center">AngularJS Toggle Password Visibility</h1>
  18.         <div class="row">
  19.                 <div class="col-sm-4 col-sm-offset-4">
  20.                         <input type="{{ inputType }}" class="myInput"> <button type="button" class="btn {{ btnType }}" ng-click="showhidePassword()"><i class="fa fa-{{ icon }}"></i> {{ btnName }}</button>           
  21.                 </div>
  22.         </div>
  23. </div>
  24. <script src="app.js"></script>
  25. </body>
  26. </html>
That ends this tutorial. Happy Coding :)

Add new comment