1

<input name="name" type="text" ng-model="numbers" mandatory>

How to remove and add class of mandatory dinamically in Angular JS?

Note : "mandatory" is custom class which is implemented by me.

Thanks.

3
  • You don't need to add or remove class bu your own when you're populating with angular. Angular will do it for you. see docs.angularjs.org/api/ng/directive/ngForm Commented May 27, 2015 at 12:32
  • You are not looking for required, right? Commented May 27, 2015 at 12:34
  • Are you looking to remove and add a Class or an Attribute? Commented May 27, 2015 at 12:35

3 Answers 3

1

I don't understand what you mean by mandatory. But to apply class based on condition we do

<input name="name" type="text" ng-model="numbers" mandatory ng-class="{className: expression}">
Sign up to request clarification or add additional context in comments.

Comments

0

ng-class is option in Angularjs to add class dynamically

follow the doc for more info

https://docs.angularjs.org/api/ng/directive/ngClass

Comments

0

Assuming you want to change the class on button click

    <input name="name" type="text" ng-model="numbers" mandatory ng-class="class">
 <button ng-click="changeClass()">Change Class</button>

Now Add or remove class in controller

app.controller("con",function($scope){

        $scope.class = "class1";

        $scope.changeClass = function(){
          if ($scope.class === "class1")
            $scope.class = "class2";
          else
            $scope.class = "class1";
        };
      });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.