AngularJS will not check input of form elements that are hidden in ng-show

I work on the accordion as a registration form, and every time I move on to the next stage of registration, hiding the first part of the form and expanding it with ng-show, elements on vm.registerForm. (elementName) undefined. From what I read in the angular documentation, only the class applies to these elements, so why are they undefined when validating the form in the last step?

I could not find documentation on this.

HTML template:

<form name="vm.registerForm">
<div class="accordion">
  <div class="accordion-section expanded"
       ng-show="vm.formList.indexOf('playerInfo') != -1 || vm.formList.indexOf('login') != -1">
    <div class="section-title" ng-click="vm.gotoMode('playerInfo')">User info</div>
    <div class="section-content" ng-show="vm.mode.playerInfo || vm.mode.login">

      <div class="flex-form">
        <div class="flex-form-group" ng-if="vm.mode.playerInfo">
          <label>Already have an account? Just log in!</label>
          <div class="button" ng-click="vm.gotoMode('login')">Log in</div>
        </div>
        <div class="flex-form-group dummy"></div>
        <div class="flex-form-group dummy"></div>
        <div class="flex-form-group dummy"></div>
      </div>

      <div class="flex-form" ng-if="vm.mode.playerInfo">
        <div class="flex-form-group">
          <label for="user_name">Username</label>
          <input username-checker type="text" ng-model="vm.username" name="user_name">
        </div>
        <div class="flex-form-group">
          <label for="first">First name</label>
          <input type="text" ng-model="vm.firstName" name="first" ng-required="true">
        </div>
        <div class="flex-form-group">
          <label for="last">Last name</label>
          <input type="text" ng-model="vm.lastName" name="last" ng-required="true">
        </div>
        <div class="flex-form-group" ng-if="vm.gameManager.game.name == 'League of Legends'">
          <label for="summoner">Summoner Name</label>
          <input type="text" ng-model="vm.summoner" name="summoner" summoner>
          <br>
        </div>
        <div class="flex-form-group">
          <label for="email">Email</label>
          <input type="email" ng-model="vm.email" name="email" ng-required="true">
        </div>
        <div class="flex-form-group">
          <label for="birthday">Day of birth (MM/DD/YYYY)</label>
          <input type="text" ng-model="vm.birthday" name="birthday" ng-required="true" birthdate>
        </div>
        <div class="flex-form-group">
          <select ng-model="vm.sex" name="sex" ng-required="true">
            <option value="">Gender</option>
            <option value="male">Male</option>
            <option value="female">Female</option>
            <option value="other">Other</option>
            <option value="not_specified">Prefer not to answer</option>
          </select>
        </div>
        <div class="flex-form-group">
          <label for="zip">Zip code</label>
          <input type="text" ng-model="vm.zip" name="zipcode" ng-required="true" zipcode>
        </div>
        <div class="flex-form-group">
          <label for="phone">Phone number (optional)</label>
          <input type="text" ng-model="vm.phone" name="phone" ng-required="false" phone-number>
        </div>
        <div class="flex-form-group">
          <label for="password">Password</label>
          <input type="password" ng-model="vm.password" name="password" ng-minlength="6" ng-maxlength="20">
        </div>
        <div class="flex-form-group">
          <label for="password2">Password (again)</label>
          <input type="password" ng-model="vm.password2" name="password2" ng-minlength="6" ng-maxlength="20">
        </div>
        <div class="flex-form-group">
          <div class="checkbox-holder">
            <input class='checkbox' type="checkbox" name='showPlayerName' ng-model="vm.showPlayerName">
            <label for="full_name">Show Real Name on Bracket</label>
          </div>
        </div>
        <div class="flex-form-group">
          <div class="checkbox-holder">
            <input class='checkbox' type="checkbox" name='type' value="true" ng-model="vm.type">
            <label for="spec">Spectator</label>
          </div>
        </div>
        <div class="flex-form-group">
          <div class="checkbox-holder">
            <input class='checkbox' type="checkbox" id='accept' value="true" ng-required="vm.regMode != 'user'"
                   ng-model="vm.accept">
            <label for="accept">Accept Terms of Service</label>
          </div>
        </div>
        <div class="flex-form-group">

          <a class="aside-link" href="" ng-click="vm.showTOS = true">View TOS</a>
        </div>
      </div>

      <!-- Continue -->
      <div class="button" ng-click="vm.next()" ng-if="!(vm.formList.length - 1 == vm.modeIterator)">
        Next
      </div>

      <!-- If there aren't anymore steps to complete -->
      <div class="button" ng-click="vm.next()" ng-if="(vm.formList.length - 1 == vm.modeIterator)">
        Confirm
      </div>
    </div>
  </div>
  <div class="accordion-section" ng-class="{expanded: vm.mode.teams}" ng-if="vm.formList.indexOf('payment') != -1">
    <div class="section-title" ng-click="vm.gotoMode('teams'); vm.gotoTeamMode('preference')">Teams</div>
    <div class="section-content">
    ...
    </div>
  </div>
</div>

Confirmation code:

function userFormValid() {
  var valid = false;
  if (vm.regMode == "user") {
    valid = true;
  } else if (vm.regMode == "anon" || vm.regMode == "admin") {
    // All the parts of the form.
    valid = vm.registerForm.zipcode.$valid && vm.registerForm.first.$valid && vm.registerForm.last.$valid;
    valid &= vm.registerForm.email.$valid && vm.registerForm.sex.$valid && vm.registerForm.birthday.$valid;
    valid &= (vm.accept == true) && vm.registerForm.user_name.$valid && vm.registerForm.password.$valid;
    valid &= vm.password == vm.password2;
    valid &= vm.registerForm.password2.$valid;

    // Let show a message if this is invalid
    if (vm.password != vm.password2) {
      vm.showMessage('Passwords must match.');
      return false;
    }
  }


  return valid;
}

Running the above function at the end of registration does not display any of the specified elements.

+4
source share
1

ng - DOM. - , angular.js FromController NgModelController, .

+1

Source: https://habr.com/ru/post/1651852/


All Articles