Search code examples
javascriptangularjsformsvalidationangularjs-scope

How to programmatically set ng-required to true (via controller) in angularjs form


I have a form and an input field, I want to make it required based on specific condition, but I can't change the html page, I have to set the make the field required to true dynamically inside the controller. Does angular provide any way to do set an input field to required field.

Also i don't have attribute

ng-required 

present for the input field.


Solution

  • I'm not sure I understand that you are not able to change the HTML page? Can you set the ng-required attribute to something in $scope?

    e.g. HTML

    <input type="text" ng-required="value">
    

    e.g. Controller

    $scope.value = true;
    

    If you can't set it via a $scope variable, it might not be possible due to the ng-required not referring to anything. If you need a more dynamic method of returning true/false you can set ng-required to a function and have the function return true or false based on whatever you want the function to check. Can't help more without more information, sorry.