Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular passes undefined when a field is blank

I have a form with mandatory and non mandatory fields. If I do not enter any data into the non mandatory fields, undefined is passed by default instead of a null string/ empty object/ empty Array. As a result, this variable is not stored in the DB at all

Is there any Generic way to pass an empty string/object/array instead of undefined when the field is left blank?

like image 928
Abhijith S Avatar asked Oct 18 '22 20:10

Abhijith S


1 Answers

You can initialize the model with a space using ngInit directive. Like

<input type="text" ng-model="testModel" ng-init="testModel=' '" /> 

This way the model is always initialized to space character and will be sent in the query parameter therefore it will stored in the database. If the user types any text the value will be updated in model.

like image 130
Vivek Avatar answered Nov 15 '22 03:11

Vivek