39

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

<input data-ng-model="userInf.username" class="span12 editEmail" type="text" placeholder="[email protected]" pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}" required /> 

in the controller I have this code to disable the input element:

function($rootScope, $scope, $location, userService) { //etc $(".editEmail" ).attr("disabled", disable); // no idea how to do in angular } 

Please help.

3
  • 5
    you need to use ng-disabledCommentedJul 1, 2013 at 10:30
  • @maqjav The question specifically declares that the solution should be provided in AngularJS terms, not jQuery.
    – Stewie
    CommentedJul 1, 2013 at 10:38
  • @Stewie you are right. I delete the comment ;)
    – maqjav
    CommentedJul 1, 2013 at 10:39

6 Answers 6

82

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

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

    You need to use ng-disabled directive

    <input data-ng-model="userInf.username" class="span12 editEmail" type="text" placeholder="[email protected]" pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}" required ng-disabled="<expression to disable>" /> 
    1
    • 3
      this is old , but you were first and have the exact same correct answer I don't understand all the upvotes on the other answerCommentedMar 22, 2015 at 4:39
    5

    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"); }); } }); 
    1
    • 1
      I have to use angular 1.0.8 which doesn't have ng-disabled directive, so this was useful!CommentedApr 1, 2014 at 13:13
    2

    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="[email protected]" 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
      <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"); }); } }); 
      1
      • 2
        A bit of explanation would be helpful.
        – ouflak
        CommentedDec 24, 2014 at 10:23
      0
      <input data-ng-model="userInf.username" class="span12 editEmail" type="text" placeholder="[email protected]" pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}" required ng-disabled="true"/> 
      1
      • A bit of explanation would be helpful.CommentedJul 21, 2016 at 22:03

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.