How to disable an input box using angular.js

后端 未结 6 559
名媛妹妹
名媛妹妹 2020-12-14 06:00

I am using this field for an edit view and a create view



        
相关标签:
6条回答
  • 2020-12-14 06:25

    You need to use ng-disabled directive

    <input data-ng-model="userInf.username" 
           class="span12 editEmail" 
           type="text" 
           placeholder="me@example.com" 
           pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}" 
           required 
           ng-disabled="<expression to disable>" />
    
    0 讨论(0)
  • 2020-12-14 06:30

    I created a directive for this (angular stable 1.0.8)

    <input type="text" input-disabled="editableInput" />
    <button ng-click="editableInput = !editableInput">enable/disable</button>
    
    app.controller("myController", function(){
      $scope.editableInput = false;
    });
    
    app.directive("inputDisabled", function(){
      return function(scope, element, attrs){
        scope.$watch(attrs.inputDisabled, function(val){
          if(val)
            element.removeAttr("disabled");
          else
            element.attr("disabled", "disabled");
        });
      }
    });
    
    0 讨论(0)
  • 2020-12-14 06:31
    <input type="text" input-disabled="editableInput" />
    <button ng-click="editableInput = !editableInput">enable/disable</button>
    
    app.controller("myController", function(){
      $scope.editableInput = false;
    });
    
    app.directive("inputDisabled", function(){
      return function(scope, element, attrs){
        scope.$watch(attrs.inputDisabled, function(val){
          if(val)
            element.removeAttr("disabled");
          else
            element.attr("disabled", "disabled");
        });
      }
    });
    
    0 讨论(0)
  • 2020-12-14 06:33

    Use ng-disabled or a special CSS class with ng-class

    <input data-ng-model="userInf.username"  
           class="span12 editEmail" 
           type="text"  
           placeholder="me@example.com"  
           pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}" 
           required 
           ng-disabled="{expression or condition}"
    />
    
    0 讨论(0)
  • 2020-12-14 06:33
    <input data-ng-model="userInf.username"  class="span12 editEmail" type="text"  placeholder="me@example.com"  pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}" required ng-disabled="true"/>
    
    0 讨论(0)
  • 2020-12-14 06:47

    Your markup should contain an additional attribute called ng-disabled whose value should be a condition or expression that would evaluate to be either true or false.

        <input data-ng-model="userInf.username"  class="span12 editEmail" 
           type="text"  placeholder="me@example.com"  
           pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}" 
           required 
           ng-disabled="{condition or expression}"
    />
    

    And in the controller you may have some code that would affect the value of ng-disabled directive.

    0 讨论(0)
提交回复
热议问题