Convert $ scope to string in ng-model by AngularJS

4

I am in doubt, according to the Angular tutorial ng-model only accepts a $scope if it is string , giving:

Error: [ngModel:numfmt] expected '1234' to be a number

Possible to convert to string ?

Edit:

HTML code:

<div modal="showModal" close="cancel()">
    <header class="modal-header">
      <h1 class="modal-header-title">Editar Dados</h1>
    </header>
    <section class="modal-body">
        <article class="modal-content" ng-controller="FormRegistro">
          <form class="float-label" spellcheck="false" ng-submit="credenciais_edit()">
            <legend>Edit User ID <span>{{ddata.id_usuario}}</span></legend>

              <div class="control">
                <input type="text" name="usuario" ng-model="ddata.user_nome" placeholder="{{ddata.user_nome}}" required />
                <label for="usuario">Nome do Usuário</label>
              </div>

              <div class="control medium">
                <input type="number" name="cpf" ng-model="ddata.user_cpf" placeholder="{{ddata.user_cpf}}" pattern="[0-9]+$" required />
                <label for="cpf">CPF</label>
              </div>

              <div class="control medium">
                <input type="password" name="senha" ng-model="senha" placeholder="Senha" required />
                <label for="location">Senha</label>
              </div>

              <div class="control med">
                <input type="number" name="cnh" ng-model="ddata.user_cnh" placeholder="{{ddata.user_cnh}}" pattern="[0-9]+$" required />
                <label for="cnh">CNH</label>
              </div>

              <div class="control small">
                <input type="number" name="nvacesso" ng-model="ddata.user_nv_acesso" placeholder="{{ddata.user_nv_acesso}}" required />
                <label for="nvacesso">Nível de Acesso | Default</label>
              </div>

              <div class="control small">
                <input type="number" name="rg" ng-model="ddata.user_rg" placeholder="{{ddata.user_rg}}" pattern="[0-9]+$" required />
                <label for="rg">RG</label>
              </div>

              <div class="control small">
                <input type="number" name="contato" ng-model="ddata.user_contato" placeholder="{{ddata.user_contato}}" />
                <label for="contato">Contato</label>
              </div>

              <div class="control medium">
                <input type="date" name="dtdata" ng-model="ddata.user_dt_nascimento" placeholder="{{ddata.user_dt_nascimento}}" />
                <label for="dtdata">Data de Nascimento</label>
              </div>

              <div class="control">
                <input type="text" name="address" ng-model="ddata.user_address" placeholder="{{ddata.user_address}}" />
                <label for="address">Endereço</label>
              </div>

              <div class="control small">
                <input type="text" name="bairro" ng-model="ddata.user_bairro" placeholder="{{ddata.user_bairro}}" />
                <label for="bairro">Bairro e/ou Setor</label>
              </div>

              <div class="control small">
                <select id="estado" name="estado">
                    <option value="" disabled selected>Estado</option>
                    <option value=""></option>
                    <option value="AC">Acre</option>
                    <option value="AL">Alagoas</option>
                    <option value="AM">Amazonas</option>
                    <option value="AP">Amapá</option>
                    <option value="BA">Bahia</option>
                    <option value="CE">Ceará</option>
                    <option value="DF">Distrito Federal</option>
                    <option value="ES">Espírito Santo</option>
                    <option value="GO">Goiás</option>
                    <option value="MA">Maranhão</option>
                    <option value="MT">Mato Grosso</option>
                    <option value="MS">Mato Grosso do Sul</option>
                    <option value="MG">Minas Gerais</option>
                    <option value="PA">Pará</option>
                    <option value="PB">Paraíba</option>
                    <option value="PR">Paraná</option>
                    <option value="PE">Pernambuco</option>
                    <option value="PI">Piauí</option>
                    <option value="RJ">Rio de Janeiro</option>
                    <option value="RN">Rio Grande do Norte</option>
                    <option value="RO">Rondônia</option>
                    <option value="RS">Rio Grande do Sul</option>
                    <option value="RR">Roraima</option>
                    <option value="SC">Santa Catarina</option>
                    <option value="SE">Sergipe</option>
                    <option value="SP">São Paulo</option>
                    <option value="TO">Tocantins</option>
                </select>
                <label for="estado">Estado</label>
              </div>

              <div class="control medium">
                <select id="cidade" name="cidade" id="cidade" ng-model="ddata.id_cidade">
                    <option placeholder="" disabled="disabled"></option>
                </select>
                <label for="cidade">Cidade</label>
              </div>

              <div class="control medium">
                <input type="submit" name="login" id="login" value="Atualizar os Dados" ng-click="ok()" />
              </div>

              <div class="control medium">
                <input type="button" class="btn-default" value="Cancelar" ng-click="cancel()" />
              </div>

          </form>
        </article>
    </section>
    <footer class="modal-footer wrapper">
      <code>O registro é obrigatório dados reais do Usuário.</code>
    </footer>
</div>

</div>
</div>

app.js link JSON with PHP and AngularJS Requesting data from MySql, passing in an array and sending via POST to AngularJS via JSON.

Edit:

I added this code by taking console.log() :

//console.log(status + ' - ' + angular.toJson(data));
            $scope.user = angular.toJson(data);
            $scope.ddata = angular.fromJson($scope.user);
    
asked by anonymous 07.12.2015 / 02:38

1 answer

2

Actually, your problem happens because you are using input number , this field yes only accepts values to type number, so you should use the conversion. The fields of text , for example, accept any format, so you do not have to worry.

In the AngularJS documentation itself, see here , there is a resolution and much more practical. Just add a new .directive to your project and call it on every input number you have. Example:

.directive('toNumber', function() {
  return {
    require: 'ngModel',
    link: function(scope, element, attrs, ngModel) {
      ngModel.$parsers.push(function(value) {
        return '' + value;
      });
      ngModel.$formatters.push(function(value) {
        return parseFloat(value, 10);
      });
    }
  };
});

And in your HTML:

<input to-number type="number" name="cpf" ng-model="ddata.user_cpf" placeholder="{{ddata.user_cpf}}" pattern="[0-9]+$" required />
    
26.01.2016 / 12:04