Ramya Ramya - 3 months ago 5x
AngularJS Question

remove validations dynamically using angular JS

Am creating a web application. It has a form with 5 mandatory input fields.It has 2 buttons. One is submit & another one is save for later.
When I click on submit, the form should validate all the mandatory fields & save the input given by the user. This is working fine for me.

When I click on "save for later", only the first input field should be mandatory. All other fields should be changed as optional. How to achieve this using angular js?


See ng-required.

The directive sets the required attribute on the element if the Angular expression inside ngRequired evaluates to true.

Example below:

  .module('exampleApp', [])
  .controller('ExampleController', ExampleController);

function ExampleController() {
  var vm = this;
  vm.required = false;
<!DOCTYPE html>
<html ng-app='exampleApp'>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>

<body ng-controller="ExampleController as vm">
  <form name="vm.form">
    <label for="required">Toggle required:</label>
    <input type="checkbox" ng-model="vm.required" id="required" />
    <label for="input">This input must be filled if `required` is true:</label>
    <input type="text" ng-model="model" id="input" name="input" ng-required="vm.required" />
    <p>required status: {{vm.required}}</p>
    <p>form error: {{vm.form.input.$error}}</p>