Angularjs下降验证不起作用

时间:2016-09-02 14:12:52

标签: angularjs

以下是代码段:

<select name="userSchool" id="userSchool" ng-model="data.model" ng-init="data.model='0'" required>
                    <option value="0">Select School</option>
                    <option ng-repeat="option in data.availableOptions" value="{{option.id}}">{{option.name}}</option>
                </select>



<md-button type="submit" class="md-raised md-accent submit-button" aria-label="Sign up"
                       ng-disabled="signupForm.$invalid || signupForm.$pristine" ng-click="signupNow()">
                SIGN UP
            </md-button>

我只是简单地使用必需的名字,如

<input type="text" name="firstName" ng-model="vm.form.firstName" placeholder="Enter first name"
                       required>

它可以使用名字而不是userSchool。

请建议。

1 个答案:

答案 0 :(得分:0)

由于您已使用值初始化它,因此已满足必需属性。您需要使用没有值的选项并删除ng-init以使所需的验证正常工作。

&#13;
&#13;
angular.module('app', [])
  .controller('ctrl', function($scope) {
    $scope.data = {};
    $scope.data.model = undefined;
    $scope.data.availableOptions = [{
      id: 1,
      name: 'School 1'
    }, {
      id: 2,
      name: 'School 2'
    }, {
      id: 3,
      name: 'School 3'
    }];
    $scope.submit = function() {
      console.log('submitted: ', $scope.data.model);
    };
  });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
<form name="theForm" ng-app="app" ng-controller="ctrl" ng-submit="submit()">
  <select name="userSchool" id="userSchool" ng-model="data.model" required>
    <option value="">Select School</option>
    <option ng-repeat="option in data.availableOptions" value="{{option.id}}">{{option.name}}</option>
  </select>
  <input type="submit" value="Submit">
</form>
&#13;
&#13;
&#13;