2015-03-24 14 views

Trả lời

6

bạn chỉ có thể làm

<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"> 

đọc here

+0

Cảm ơn bạn đã trả lời của bạn, tôi đã cố gắng này nhưng tôi có như để có những gì tôi đã thêm vào trong câu trả lời đã chỉnh sửa, làm thế nào có thể làm cho nó ra khỏi điều này. – Karthik

+0

Để làm tròn câu trả lời tuyệt vời này, đây là thông tin Bootstrap được yêu cầu: http://getbootstrap.com/components/#input-groups Sử dụng một nhóm-đầu vào-addon. – Stone

0

Bạn muốn hộp đầu vào để được ẩn hoặc hiển thị/ẩn mật khẩu?

Dưới đây là những gì tôi đã làm cho đến bây giờ

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

2

Sử dụng một đầu vào độc đáo và chuyển đổi loại hình của mình như thế này:

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

khiển

// Set the default value of inputType 
    $scope.inputType = 'password';  
    // Hide & show password function 
    $scope.hideShowPassword = function(){ 
    if ($scope.inputType == 'password') 
     $scope.inputType = 'text'; 
    else 
     $scope.inputType = 'password'; 
    }; 

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

6

AngularJS/UI Bootstrap Giải pháp

  • Sử dụng Bootstrap để hiển thị một biểu tượng bên trong lĩnh vực đầu vào.
  • Hãy chắc chắn rằng các biểu tượng có style="cursor: pointer; pointer-events: all;"
  • Sử dụng ng-if để hiển thị/ẩn các hình thức khác nhau, nơi các <label type="password"> vs <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; 
    } 
}); 

Give it là spin với plunker: http://plnkr.co/edit/oCEfTa?p=preview

+0

Tại sao bạn sử dụng: $ scope.params = {}; ? – grep

+0

@grep '$ scope.params' chỉ là một đối tượng để giữ các biến. '$ scope.params.password' được đặt bởi' ng-model = "params.password" của góc cạnh trong 'html. Bạn có thể có một nút đăng nhập hoặc đăng ký kích hoạt một số chức năng có thể truy cập các biến trong $ scope.params. –

23

Bạn có thể tự động thay đổi kiểu đầu vào với các chỉ ng-attr-type. Ví dụ:

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

bạn có thể liên kết showPassword với sự kiện nhấp và chuyển đổi sự kiện.

+0

Đó là một aswer chính xác! – lesimoes

+0

là 100% HTML hợp lệ, bạn cũng cần phải thêm 'type =" password "', nhưng tại thời gian chạy góc này sẽ được thay thế. Chỉ cần một mẹo cho bất kỳ ai có bất kỳ công cụ xác thực HTML tự động nào. –

+1

** HTML ** ' {{typePassword? 'Ẩn': 'Hiển thị'}}
' ** Trong Bộ điều khiển góc ** '$ scope.togglePassword = function() { $ scope.typePassword =! $ Scope.typePassword; }; ' – Ferie

3

Bạn cũng có thể thử này một

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

Dưới đây là một bản demo làm việc:

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

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

 
});
<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>

Các vấn đề liên quan