Convert $Scope to string in ng-model by Angularjs

Asked

Viewed 933 times

4

I am in doubt, according to the tutorial of Angular the ng-model only accepts one $scope if he goes string, giving:

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

Possible then 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>

Link to app js. JSON with PHP and Angularjs Requesting Mysql data, passing an array and sending via POST to Angularjs via JSON.


Edit:

I added this code apart from the console.log():

//console.log(status + ' - ' + angular.toJson(data));
            $scope.user = angular.toJson(data);
            $scope.ddata = angular.fromJson($scope.user);
  • Have you tried using the function .toString() javascript?

  • Yes and also the string() javascript but without success.

  • Share a little more code, Eduardo, so we can see straight. :)

  • Take a look here: https://docs.angularjs.org/error/ngModel/numfmt

  • I’m leaving the code, I thought I’d do the ng-value but I don’t know if my app.js will pick up the value or just the ng-model.

  • 1

    Already tried Json.stringfy($Scope)?

  • I don’t know if I’m missing the interpretation of the code: var stringdata = angular.ToJson(data); $scope.ddata = angular.fromJson(JSON.stringify(stringdata));

Show 2 more comments

1 answer

2


Actually your problem happens because you are using a input number, this field yes accepts only values to type number, so you should use the conversion. The fields of text, for example, accept any format, so you need not 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 in each 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 />
  • Opa really liked the code @Celsom and it worked perfectly! Even when sending to PHP he understands that it is String, but only explains the difference of putting the - of to-number I realized that you put when you’re in HTML and Angularjs code node you strip the crossbar.

  • 1

    This is why the definition of directives requires you to replace uppercase letters with lowercase letters and add - before where the uppercase letter was. For example ngClick -> ng-click, ngModel -> ng-model; and so on. By convention, we use upper case name distinction to make it more organized: myDirective -> my directive. What you cannot do is: Minhadiretiva, as it will become an invalid name: -my directive, not being recognized as a definition of a directive.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.