禁用angular6的按键输入?

时间:2018-10-31 15:08:23

标签: javascript typescript angular6

我试图仅将attribute disable设置为发生按键事件的输入,并且不应禁用其余输入吗?

abc.component.html

<input type="text" (keydown)="disableIt($event)"> // <-- If keydown occurs 
                                                   //here, disable it
<input type="text" (keydown)="disableIt($event)">
<input type="text" (keydown)="disableIt($event)">

abc.component.ts

public disableIt(evt) {
   if (evt.keyCode === 13 || evt.keyCode === 9) {
       evt.target.tagName.disabled  = true;
    }
}

3 个答案:

答案 0 :(得分:0)

关于

<input type="text" (keydown)="$event.preventDefault();">

答案 1 :(得分:0)

您可以使用此控制器:

ctrl($scope) {
    $scope.inputDisabled = [];
    $scope.inputs = [];
    for(let i = 0; i < numInputs; i++) {
        $scope.inputDisabled.push(false);
        $scope.inputs.push(i);
    }
    $scope.disableInput(which) {
        for(let i = 0; i < numInputs; i++) {
            $scope.inputDisabled.push(false);
        }
        $scope.inputDisabled[which] = true;
    }
}

然后使用HTML:

<input ng-repeat="i in inputs" ng-disabled="inputDisabled[i]" ng-keydown="disableInput(i)">

答案 2 :(得分:0)

您错误地选择了元素,应该使用哪个而不是keyCode。

function disableIt(event) {
  var which = event.which
  if (which===13 || which===9) {
    event.target.disabled = true
  }
}
<input type="text" onkeydown="disableIt(event)">
<input type="text" onkeydown="disableIt(event)">
<input type="text" onkeydown="disableIt(event)">