Angular seems to have a different behavior when a user enters and then clears text in an input field depending if this field is required or not.
Take a look at this example. By default the model is user = {"name":"guest","last":"visitor"}. When clearing both fields the model becomes user = {"last":""} whereas I would have expected user = {}.
Why is this the case? And, is there a way to get the same behavior for both fields without making them both (not) required?
Update:
I used a workaround with watch in the end:
$scope.$watch('user',function(newValue) {
for (var prop in newValue) {
if (newValue.hasOwnProperty(prop)){
if (newValue[prop] !== undefined && newValue[prop] == "")
newValue[prop] = undefined;
}
}
}, true);
Most likely the last if-condition could be written more efficiently, but at least it seems to work as expected.