0

我在提交表单时遇到问题。

所以..我填写了公式中的所有数据,然后单击保存...数据已正确保存,但在提交后,字段被清除,然后输入接收 css,因为表单已经提交。我不想在正确保存时保留“提交”的处方集。

我的代码如下。

HTML 代码

<form name="citizenForm" ng-submit="citizensCtrl.createCitizen(citizenForm)" class="css-form" novalidate>
  <div class="form-group">
    <label for="citizen_name">Nome *</label>
    <input type="text" class="form-control" id="citizen_name" placeholder="Nome" ng-model="citizensCtrl.citizen.name" required>

  <div class="form-group">
    <label for="citizen_birthday">Nascimento *</label>
    <uib-datepicker ng-model="citizensCtrl.citizen.birthday" class="well well-sm" datepicker-options="citizensCtrl.dateOptions" required></uib-datepicker>
  </div>

  <div class="form-group">
    <label for="citizen_cell_phone">Celular *</label>
    <input type="text" class="form-control" id="citizen_cell_phone" placeholder="Celular" ng-model="citizensCtrl.citizen.cell_phone" required>
  </div>

  <div class="form-group">
    <label for="citizen_phone">Telefone *</label>
    <input type="text" class="form-control" id="citizen_phone" placeholder="Telefone" ng-model="citizensCtrl.citizen.phone" required>
  </div>

  <hr>
  <h4>Endereço</h4>
  <div class="form-group">
    <label for="citizen_address_district">Bairro *</label>
    <select id="citizen_address_district" ng-model="citizensCtrl.citizen.address.district" class="form-control"
            ng-options="district as district.name for district in citizensCtrl.districts" ng-change="citizensCtrl.getAddresses()" required>
      <option value=""> Bairro</option>
    </select>
  </div>

  <div class="form-group">
    <label for="citizen_address_public_place">Rua *</label>
    <input  type="text" ng-model="citizensCtrl.citizen.address.public_place" id="citizen_address_public_place"
            uib-typeahead="address as address.public_place for address in citizensCtrl.addresses | filter:{public_place: citizensCtrl.citizen.address.public_place} | limitTo:5"
            typeahead-min-length="6" typeahead-select-on-exact="true"
            typeahead-on-select="citizensCtrl.getZipCodes()"
            typeahead-loading="loadingLocations" 
            ng-disabled="!citizensCtrl.citizen.address.district.id"
            typeahead-no-results="noResults" class="form-control" required>
    <i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
    <div ng-show="noResults">
      <i class="glyphicon glyphicon-remove"></i> Endereço não encontrado
    </div>
  </div>

  <div class="form-group">
    <label for="citizen_address_zip_code">CEP *</label>
    <select id="citizen_address_zip_code" ng-model="citizensCtrl.citizen.address.zip_code"
            ng-disabled="!citizensCtrl.citizen.address.public_place.id" class="form-control"
            ng-options="zip_code as zip_code.number for zip_code in citizensCtrl.zip_codes" required>
      <option value="">CEP</option>
    </select>
  </div>

  <div class="form-group">
    <label for="citizen_address_number">Numero *</label>
    <input type="text" class="form-control" id="citizen_address_number" placeholder="Numero" ng-model="citizensCtrl.citizen.address.number" required>
  </div>

  <div class="form-group">
    <label for="citizen_address_complement">Complemento</label>
    <input type="text" class="form-control" id="citizen_address_complement" placeholder="Complemento" ng-model="citizensCtrl.citizen.address.complement">
  </div>

  <input type="submit" class="btn btn-primary" value="Criar" />
  <input type="reset" class="btn btn-default" ng-click="citizensCtrl.defineCitizen()" value="Limpar Formulário" />
</form>

CSS 代码

.css-form.ng-submitted .ng-invalid{
  border: 1px solid #FA787E;
}

AngularJS 控制器功能

self.createCitizen = function(form){
  if(form.$valid){
    $http.post(apiUrl + endpoint, self.citizen).then(function(response){
      alertsService.add('success', 'Morador criado com sucesso!');
      self.defineCitizen();
    }, function(error){
      alertsService.add('danger', 'Oops.. Alguma coisa deu errado. Contate o administrador.');
    });
  }else{
    alertsService.add('danger', 'Você precisa preencher todos os campos obrigatórios.');
  }
};
4

1 回答 1

0

form.$setPristine();在创建功能上使用

self.createCitizen = function(form){
  if(form.$valid){
    $http.post(apiUrl + endpoint, self.citizen).then(function(response){
      alertsService.add('success', 'Morador criado com sucesso!');
      form.$setPristine();
      self.defineCitizen();
    }, function(error){
      alertsService.add('danger', 'Oops.. Alguma coisa deu errado. Contate o administrador.');
    });
  }else{
    alertsService.add('danger', 'Você precisa preencher todos os campos obrigatórios.');
  }
}; 

它非常适合我。
谢谢你们

于 2016-06-07T21:01:16.843 回答