2017-09-21 1 views
0

다음 코드가 있습니다.함수 실행시 입력 유형을 비활성화하고 활성화하는 방법은 무엇입니까?

HTML :

<div ng-controller="Test"> 
     <input type="text" class="message-input" id="testId" placeholder="Enter message" ng-model="textMessage" ng-disabled="isDisabled" autofocus /> 
     <br><br> 
     <button type="submit" value="Submit" ng-click="enteredMessage(textMessage)">Submit</button> 
    </div> 

JS : 이것을 실행하면, 내가 할 수 자동 초점 기본적으로 일부 텍스트를 입력받을 수 있습니다

var app = angular.module('app',[]); 
app.controller('Test',function($scope){ 
$scope.isDisabled = false; 
    $scope.enteredMessage = function(msg){ 
    alert("Entered Message is: "+msg); 
    console.log("your input should be in diabled mode now"); 
    $scope.isDisabled = true; 
    console.log("your input should be in enabled mode now with auto-focused"); 
    } 
}); 

, 그것은 괜찮습니다.

내가 실행 ng-click 기능을 내 input 유형을 disable 할 및 함수 실행이 텍스트를 입력하는 자동차를 중심으로 수행 한 후 다음이 enabled을해야 의미합니다.

내가 잘못하고있는 방식과이를 수행하는 방법에 대해 미리 알려주세요. 생성 된 Fiddle.

+0

나는 당신의 문제가 무엇인지 이해할 수 없다. 입력 된 메시지 기능이 시작될 때 입력을 비활성화하고 입력 된 메시지 기능이 종료 될 때 다시 활성화 하시겠습니까? – firegloves

+0

경고를 보낼 때 경고 기능은 사용자가 UI의 다른 부분에 액세스하는 것을 방지합니다. 즉, 경고가 열리는 동안 입력이 이미 "비활성화"되어 있고 경고가 닫히면 "사용 가능"으로 설정됩니다. https://developer.mozilla.org/en-US/docs/Web/API/Window/alert –

+0

@firegloves, 예 ... 또한 입력을 다시 활성화하는 동안 자동으로 초점을 맞춰야합니다 (수동 입력을 클릭하는 대신 텍스트를 입력하면 자동으로 초점이 맞추어 져있어 텍스트를 직접 입력 할 수 있습니다.) – Guna

답변

0

사실 alert()에는 사용자가 확인 버튼을 클릭하거나 닫을 때 실행할 콜백이 없습니다. 대신 콜백을 지원하는 모달 또는 다른 종류의 지시문을 사용해야합니다 (아래 예 참조). 경고가 표시되면 비활성화하고 확인을 클릭하면 활성화됩니다. 확인 해봐.

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

app.controller('Test',function($scope){ 
$scope.isDisabled = false; 
    $scope.enteredMessage = function(msg){ 
    $scope.isDisabled = true; 
    alert("Entered Message is: "+msg); 
    console.log("your input should be in diabled mode now"); 

    //$scope.msg=""; 
    console.log("your input should be in enabled mode now with autofocus"); 
    $scope.isDisabled = false; 
} 

}); 

나는 ui.bootstrap에게 모달 또는 ngSweetAlert를 사용하는 것이 좋습니다.

업데이트 대신 함수 호출이 경고됩니다. 내가 코멘트에서 언급 한 바와 같이 FIDDLE

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

    app.controller('Test',function($scope, $window){ 
    $scope.isDisabled = false; 
     $scope.enteredMessage = function(msg){ 
     $scope.isDisabled = true; 
     alert("Entered Message is: "+msg); 
     console.log("your input should be in diabled mode now"); 
     $scope.showConfirm(); 
     //$scope.msg=""; 
     //console.log("your input should be in enabled mode now with autofocus"); 
     //$scope.isDisabled = false; 
     } 

     $scope.showConfirm = function(){ 
      if(confirm('your input should be in enabled mode now with autofocus if click Ok')){ 
      $scope.isDisabled = false; 
      var inputBox = $window.document.getElementById('testId'); 
     inputBox.focus(); 
      } 
     } 

    }); 
+0

코드에 새로운 변경 사항이 표시되지 않습니다. 실제로 그것은 모든 각도 기능 일 수 있으며 경고 메시지가 아닙니다. 방금 경고 메시지를 표시하는 함수를 작성했습니다. 내 요구 사항은 각도 함수 실행을위한 입력 유형이어야합니다. 게시물을 편집했습니다. – Guna

+0

차이점은 상단에'$ scope.isDisabled = true'를 추가하고 하단에는'$ scope.isDisabled = false'를 추가합니다. –

+0

만약 당신의 의도가 일부 기능이 경고 ... 요 기능 또는 종려가 실행을 끝내고, 다시 활성화 입력을 해제해야합니다. 그게 왜 순서에있는거야. –

0

, 그래서 비활성화 모드의 입력을 넣어 Alert이미 장애인 당신의 UI는 유용하지 않습니다. 경고가 당신이 좋아하는 뭔가를 할 수 없어 한 후 두 번째 문제의 경우, 입력에 집중할 다시 입력 요소를

예 수 있도록, 기능을 시간을 사용할 수 있습니다

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

app.controller('Test', function($scope, $window) { 
    $scope.enteredMessage = function(msg) { 
    alert("Entered Message is: " + msg); 
    var inputBox = $window.document.getElementById('testId'); 
    inputBox.focus(); 
    console.log("your input should now be in focus"); 
    } 
}); 
0

다음 :

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

app.controller('Test',function($scope,$timeout){ 
    $scope.isDisabled = false; 
    $scope.enteredMessage = function(msg){ 
    $scope.isDisabled = true; 
    alert("Entered Message is: "+msg); 
    console.log("your input should be in diabled mode now"); 

    //5 seconds delay 
    $timeout(function(){ 
     $scope.isDisabled = false; 
     },5000); 
    console.log("your input should be in enabled mode now with autofocus"); 
} 

}); 
+0

예 괜찮 았으나 활성화 또는 시간 초과 후 자동 초점 모드가 아닙니다. – Guna

관련 문제