我有密碼和一個輸入字段與輸入字段
現在一個按鈕,如果輸入字段的類型是密碼即type="password"
,然後在該按鈕的點擊它應該成爲文本即type="text"
如果我再次點擊同一按鈕,它應該改變type="password"
AngularJs - 顯示隱藏密碼的單擊事件使用指令
意味着它應該觸發類型的輸入元素的value
我已經完成了這個使用控制器,它與控制器工作正常。以下爲使用控制器
但不是控制器的工作代碼,如果我想使用的指令那麼如何使用指令
目的來處理此切換的條件 - 我想使用多輸入元素此功能
HTML
<div class="input-group">
<label>Password</label>
<input type="{{inputType}}" class="form-control" ng-model="password" />
<span ng-click="show()">show</span>
</div>
控制器
$scope.inputType = 'password';
$scope.show = function() {
if ($scope.inputType === 'password') {
$scope.inputType = !$scope.inputType;
$scope.inputType = 'text';
} else {
$scope.inputType = !$scope.inputType;
$scope.inputType = 'password';
}
}
我試着用指令 - 下面是我的審判代碼
我沒有得到如何使用指令
指令
.directive('myDir', function() {
require: 'ngModel',
link: function (scope, element, attrs) {
console.log(attrs.type);
attrs.type = 'password'
// how to call this below code on click event or on click of <span>
which I am using for password
if (attrs.type === 'password') {
attrs.type = !attrs.type;
attrs.type = 'text';
} else {
attrs.type = !attrs.type.inputType;
attrs.type = 'password';
}
}
})
HTML使用改變<input />
元素的類型指令
<div class="input-group">
<label>Password</label>
<input my-dir type="" class="form-control" ng-model="password" />
<span ng-click="show()">show</span>
</div>
你希望別人爲你創建一個指令? – Tuhin
@Tuhin我已經添加了我的試用指令代碼 - 我沒有得到如何使用指令 –
更改類型,因此沒有內置指令。你需要編寫一個自定義指令。看看這個。你可能會得到一些想法http://stackoverflow.com/questions/39247705/ng-type-show-password-directive-on-input-element-in-angularjs – Tuhin