When I execute this script, aForm.aField.$error contains {"required":true} BUT $scope.fruit is not set to undefined even though it is not in the md-option values. So the "required" validation is done but not the set to undefined.
<!doctype html>
<html ng-app="anApp">
<head>
<link rel="stylesheet" href="bower_components/angular-material/angular-material.min.css">
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-messages/angular-messages.min.js"></script>
<script src="bower_components/angular-material/angular-material.js"></script>
<script src="bower_components/angular-animate/angular-animate.min.js"></script>
<script src="bower_components/angular-aria/angular-aria.min.js"></script>
<script>
angular.module('anApp', ['ngMessages', 'ngMaterial'])
.controller('aController', function ($scope, $timeout) {
$scope.fruitBasket = [{name:"apple", id:1}];
$scope.fruit = 100;
$scope.required = true;
});
</script>
</head>
<body ng-controller="aController">
{{aForm.aField.$error}}<br> [{{fruit}}]
<br>
<form name="aForm">
<md-input-container>
<label>Fruit</label>
<md-select name="aField" ng-required="required" ng-model="fruit">
<md-option ng-repeat="unFruit in fruitBasket" ng-value="unFruit.id"> {{unFruit.name}} </md-option>
</md-select>
<div ng-messages="aForm.aField.$error">
<div ng-message="required">Fruit required</div>
</div>
</md-input-container>
</form>
</body>
</html>
But when I execute this script, $scope.fruit is set to undefined after the timeout is executed:
<!doctype html>
<html ng-app="anApp">
<head>
<link rel="stylesheet" href="bower_components/angular-material/angular-material.min.css">
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-messages/angular-messages.min.js"></script>
<script src="bower_components/angular-material/angular-material.js"></script>
<script src="bower_components/angular-animate/angular-animate.min.js"></script>
<script src="bower_components/angular-aria/angular-aria.min.js"></script>
<script>
angular.module('anApp', ['ngMessages', 'ngMaterial'])
.controller('aController', function ($scope, $timeout) {
$scope.fruitBasket = [{name:"apple", id:1}];
$scope.fruit = 100;
$scope.required = false;
$timeout(function() {
$scope.required = true;
}, 1000);
});
</script>
</head>
<body ng-controller="aController">
{{aForm.aField.$error}}<br> [{{fruit}}]
<br>
<form name="aForm">
<md-input-container>
<label>Fruit</label>
<md-select name="aField" ng-required="required" ng-model="fruit">
<md-option ng-repeat="unFruit in fruitBasket" ng-value="unFruit.id"> {{unFruit.name}} </md-option>
</md-select>
<div ng-messages="aForm.aField.$error">
<div ng-message="required">Fruit required</div>
</div>
</md-input-container>
</form>
</body>
</html>
When I look at angular's code, when invoking $$runValidators, there's this "prevValid" condition that seems to influence the "set to undefine" mecanism.
So it seems md-select (because of angular) is not always setting the model to undefined when it is not a valid value.
What should I do if I want my first script (the one without the timeout) to set the model to undefined because its value is not in the md-option values?
Are these behaviors documented somewhere?
What are the rules?