0

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?

2

1 Answer 1

1

well finally I've found the answer in angular doc...

Runs each of the registered validators (first synchronous validators and then asynchronous validators). If the validity changes to invalid, the model will be set to undefined, unless ngModelOptions.allowInvalid is true. If the validity changes to valid, it will set the model to the last available valid $modelValue, i.e. either the last parsed value or the last value set from the scope.

It's the fact that the validity state changes from true to false when the field is set to required.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.