Two way binding not working in AngularJS form in Service Now platform

162 Views Asked by At

I'm working at a form on the ServiceNow platform, using AngularJS. It seems like something quite simple like two way binding is not working.

Here's a simplified snippet of my html:

<form name="vulnerabilityForm" ng-submit="addVulnerability(newVulnerability, newSeverity)">
  <div class="col-sm-10"> 
    <input type="text" class="form-control" id="vulnerability" ng-model="newVulnerability" required> 
  </div>
  <div class="col-sm-10">
    <select ng-options="option.value as option.name for option in severityOptions" class="form-control" id="severity" ng-model="newSeverity" required> 
      <option value="">Select Severity</option> 
    </select> 
  </div>
</form>

And the client script:

$scope.vulnerabilities = [];

$scope.newVulnerability = '';
$scope.newSeverity = null;

$scope.addVulnerability = function(newVulnerability, newSeverity) {  
    $scope.vulnerabilities.push({
            description: newVulnerability,
            severity: newSeverity
        });

   // reset input fields
    $scope.newVulnerability = "";
    $scope.newSeverity = "";
    $scope.vulnerabilityForm.$setPristine();
    $scope.vulnerabilityForm.$setUntouched();
};

What I am trying to achieve:

  1. Update and pass the values of newVulnerability and newSeverity to the client script via ng-submit
  2. reset the form upon submission (hence emptying the text input and resetting the select)

Problems:

  1. It's my understanding that ng-submit="addVulnerability()" should just submit the values, which are updated via the ng-model directive which enables the two way binding. However, the values won't update for some reason. I solved this by explicitly passing the values in the submit, like so - ng-submit="addVulnerability(newVulnerability, newSeverity)". This seems to be working, but not via two way binding.

  2. Resetting the form does not work - again, shouldn't resetting the variables to empty strings automatically update the UI as well?

What am I missing?

0

There are 0 best solutions below