I've noticed something odd in AngularJS 1.7.9.
If I use the code <input type="text" ng-model="object.property">, and empty the control (i.e., set its value to an empty string), then object.property also gets set to an empty string – which is what I want and expect.
However, if I add the required attribute to the <input>, and empty the control, then property gets removed from object entirely!
Is this intended behavior? If so, is there a workaround?
The DEMO
angular.module("app",[])
.controller('requiredTestController', ['$scope', function ($scope) {
$scope.user = {
name: 'delete this text'
};
$scope.userWithRequiredName = {
name: 'delete this text'
};
}]);
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app" ng-controller="requiredTestController">
<p>user: {{ user }}</p>
<input type="text" ng-model="user.name">
<hr>
<p>userWithRequiredName: {{ userWithRequiredName }}</p>
<input type="text" ng-model="userWithRequiredName.name" required>
</body>