$setPristine() 不适用于角度材质形式

Ris*_*ain 5 angularjs angular-material

我正在用有角材料制作一个简单的表单来添加帖子,我还有一个取消按钮来清除表单。我尝试使用$setPristine()但在调用clearForm函数后输入字段仍然显示红色。我遵循了这里的一些答案,但这并没有解决我的问题。这是我的 index.html 和 app.js

<form id="addForm" name="myForm">
                <fieldset id="addField">
                    <legend id="addFormLegend">Create Post</legend>
                    <md-input-container class="md-block">

                        <md-icon md-svg-src="name.svg"></md-icon>
                        <input ng-model="post.name" type="text" placeholder="Name" ng-required="true">
                    </md-input-container>
                    <md-input-container class="md-block">

                        <md-icon md-svg-src="desc.svg"></md-icon>
                        <input ng-model="post.desc" type="text" placeholder="Description" ng-required="true">
                    </md-input-container>
                    <md-input-container class="md-block">

                        <md-icon md-svg-src="link.svg"></md-icon>
                        <input ng-model="post.url" type="url" placeholder="Url " ng-required="true">
                    </md-input-container>
                    <md-button ng-click="clearForm()" class="md-raised md-primary" id="cancelButton"> Cancel </md-button>
                    <md-button ng-click="createPost(post)" class="md-raised md-primary" ng-disabled="myForm.$invalid" id="addButton"> Add Post </md-button>
                </fieldset>

            </form>

app.controller('mainCtrl', ['$scope', '$firebaseArray', 'posts', function ($scope, $firebaseArray, posts) {
        $scope.posts = posts;
        $scope.showForm = false;
        var defaultForm = {
            name: "",
            desc: "",
            url: ""
        };
        $scope.demo = {};
        $scope.post = angular.copy(defaultForm);

        $scope.createPost = function (post) {
            $scope.posts.$add({
                name: post.name,
                desc: post.desc,
                url: post.url
            });
            $scope.post = {};
        };
        $scope.showAddForm = function () {
            $scope.showForm = true;
        };
        $scope.clearForm = function () {
            // $scope.showForm = false;
            $scope.myForm.$setPristine();
            $scope.post = angular.copy(defaultForm);
            console.log('empty');


        }
    }]);
Run Code Online (Sandbox Code Playgroud)

Ris*_*ain 5

实际上我查了很多答案,但没有一个有效。然后我在我的 app.js 中添加了这些行

 $scope.clearForm = function () {
            $scope.showForm = false;
            $scope.myForm.$setPristine();
            $scope.myForm.$setUntouched();
            $scope.post = {};
 }
Run Code Online (Sandbox Code Playgroud)

所以基本上添加$scope.myForm.$setUntouched(); 在$setPristine()为我工作之后。