如何使用bootstrap 3在角度js中显示/隐藏密码输入?

时间:2015-03-24 04:15:05

标签: angularjs twitter-bootstrap-3 angular-ui-bootstrap

我想使用bootstrap 3在角度js中显示/隐藏密码输入,切换选项需要在输入内部。你能提供一个片段或例子吗?万分感谢。

Sample of it

10 个答案:

答案 0 :(得分:27)

您可以使用ng-attr-type指令动态更改输入类型。例如:

<input ng-attr-type="{{ showPassword ? 'text' : 'password' }}">

您可以将showPassword绑定到click事件并进行切换。

更新(来自@ Ferie的评论)

HTML

<span class="showPassword" ng-click="togglePassword()">{{ typePassword ? 'Hide' : 'Show' }}</span><br> <input type="{{ typePassword ? 'text' : 'password' }}" name="password" placeholder="Password"> 

在角度控制器中

$scope.togglePassword = function () { $scope.typePassword = !$scope.typePassword; };

答案 1 :(得分:7)

AngularJS / UI Bootstrap解决方案

  • 使用Bootstrap的has-feedback类在输入字段中显示一个图标。
  • 确保图标有style="cursor: pointer; pointer-events: all;"
  • 使用ng-if显示/隐藏<label type="password"><label type="text">
  • 不同的表单

HTML

<!-- index.html snippet -->    

<!-- show password as type="password" -->
<div ng-if="!showPassword"
       class="form-group has-feedback">
    <label>password</label>
    <input type="password"
           ng-model="params.password"
           class="form-control"
           placeholder="type something here...">
    <span ng-if="params.password"
          ng-click="toggleShowPassword()"
          class="glyphicon glyphicon-eye-open form-control-feedback" 
          style="cursor: pointer; pointer-events: all;">
    </span>
  </div>

  <!-- show password as type="text" -->
  <div ng-if="showPassword"
       class="form-group has-feedback">
    <label>password</label>
    <input type="text"
           ng-model="params.password"
           class="form-control"
           placeholder="type something here...">
    <span ng-if="params.password"
          ng-click="toggleShowPassword()"
          class="glyphicon glyphicon-eye-close form-control-feedback" 
          style="cursor: pointer; pointer-events: all;">
    </span>
  </div>

的JavaScript

// app.js

var app = angular.module('plunker', ['ui.bootstrap']);

app.controller('MainCtrl', function($scope) {
    $scope.params = {};
    $scope.showPassword = false;
    $scope.toggleShowPassword = function() {
        $scope.showPassword = !$scope.showPassword;
    }
});

使用plunker旋转:http://plnkr.co/edit/oCEfTa?p=preview

答案 2 :(得分:6)

你可以简单地做

<input ng-show="showpassword" type="text" ng-model="password">
<input ng-hide="showpassword" type="password" ng-model="password">
<input type="checkbox" ng-model="showpassword" ng-checked="false">

阅读here

答案 3 :(得分:5)

这是一个有效的演示:

&#13;
&#13;
var app = angular.module('myApp', []);
app.controller('loginCtrl', function($scope) {
  $scope.showPassword = false;

  $scope.toggleShowPassword = function() {
    $scope.showPassword = !$scope.showPassword;
  }

});
&#13;
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form name="form" ng-app="myApp" ng-controller="loginCtrl" novalidate>
  <label>
    Password:

    <input type="password" name="password" ng-model="password" ng-attr-type="{{ showPassword ? 'text':'password'}}" />
    <div ng-click="toggleShowPassword()" ng-class="{'fa fa-eye': showPassword,'fa fa-eye-slash': !showPassword}" style="cursor: pointer;"></div>

  </label>
</form>
&#13;
&#13;
&#13;

答案 4 :(得分:4)

您也可以试试这个

<input type="{{showPassword?'text':'password'}}" />
<input type="checkbox" title="Show Password" ng-model="showPassword" ng-checked="showPassword">

答案 5 :(得分:2)

使用唯一输入并切换他的类型:

Func

控制器

<input type="{{inputType}}" placeholder="Put your password" />

http://codepen.io/gymbry/pen/fJchw/

答案 6 :(得分:0)

您希望隐藏输入框还是显示/隐藏密码?

这是我到目前为止所做的事情

HTML

<div class="btn-group">
    <input ng-hide="hidevar" id="searchinput" type="password" class="form-control" placeholder="password" >
      <span id="searchclear" ng-click="hideinpbox();">HIDE</span>
</div>

CSS:

#searchinput {
    width: 200px;
}

#searchclear {
    position:absolute;
    right:5px;
    top:0;
    bottom:0;
    height:14px;
    margin-top:7px;
    margin-right:5px;
    font-size:14px;
    cursor:pointer;
    color:#ccc;
}

http://plnkr.co/edit/TfKvlh1mM6wsNrPQKY4Q?p=preview

答案 7 :(得分:0)

简单而聪明的方式是

<div class="input-group">
  <input tabindex="2" type="password" class="form-control input-lg" ng-model="_password" placeholder="password" name="_password" required  ng-attr-type="{{ pwd_hide ? 'text':'password'}}">
  <span class="input-group-addon" ng-click="pwd_hide = !pwd_hide">{{ pwd_hide ? 'Hide':'Show'}}</span>
</div>
<span ng-show="submitted || loginForm._password.$dirty && loginForm._password.$invalid">
  <span ng-show="loginForm._password.$error.required" class="error">Please enter your password.</span>
</span>

答案 8 :(得分:0)

<input class="form-control" type="{{passwordType}}" ng-model="User.Password" name="txtPassword" ng-class="submitted?'ng-dirty':''" autofocus required /><i style="position: relative;float: right;top: -26px;right: 3px; cursor:pointer" ng-click="showPassword()" class="ti ti-eye"></i>

App.controller(&#39; myCtrl&#39;,function($ scope){     $ scope.passwordType =&#34;密码&#34 ;;

$scope.showPassword = function () {

        if ($scope.passwordType == "password") {
            $scope.passwordType = "text";
        } else {
            $scope.passwordType = "password";
        }
    }
});

答案 9 :(得分:0)

您应该尝试使用这只白色的眼睛图标

<div class="col-md-6" ng-init="paninptype='password'; iconimg='/images/icons8-invisible-32.png';">
                            <label>PAN Number</label>
                            <div class="input-group">
                                <input type="{{paninptype}}" class="form-control" name="PAN" ng-model="vm.step1.model.PAN" id="exp-pan" placeholder="e.g. FARPS XXXX G" />
                                <span class="input-group-addon">
                                    <img src="{{iconimg}}" class="pull-right" style="width:38px" ng-click="paninptype=(paninptype=='password'?'text':'password'); iconimg=(paninptype=='password'?'/images/icons8-invisible-32.png':'/images/icons8-eye-50.png');">
                                </span>
                            </div>
                        </div>