为什么不能验证ng-class?

时间:2017-02-08 13:17:47

标签: javascript angularjs validation

我有以下HTML输入:

 <input type="text" name="artist" maxlength="45"
ng-class="{'error': $scope.formAddSong.artist.$invalid && !$scope.formAddSong.artist.$pristine && $scope.formAddSong.artist.$setUntouched}"
class="form-control" placeholder="Artist"
ng-minlength="minlength"
ng-model="formSong.artist[$index]"
required autofocus>

名称为name="formAddSong"的表单。

所以,当我触摸输入并将其留空时,我不会在此元素上获得类error

1 个答案:

答案 0 :(得分:1)

试试这个:

<input type="text" name="artist" maxlength="45"
                           ng-class="{'error': $scope.formAddSong.artist.$touched && $scope.formAddSong.artist.$error}"
                           class="form-control" placeholder="Artist"
                           ng-minlength="minlength"
                           ng-model="formSong.artist[$index]"
                           required autofocus />