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:
- Update and pass the values of
newVulnerabilityandnewSeverityto the client script viang-submit - reset the form upon submission (hence emptying the text
inputand resetting theselect)
Problems:
It's my understanding that
ng-submit="addVulnerability()"should just submit the values, which are updated via theng-modeldirective 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.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?