Display values with angular

Asked

Viewed 657 times

0

I have the following code:

$http.get('/estados').success(function(retorno) {
    $scope.ufs = retorno.ufs;
    console.log(retorno);
});

and the outcome of console.log(retorno) is:

inserir a descrição da imagem aqui

Now I’m trying to show the values of this return in a <select> of HTML:

<section class="input-field col l1 s12">
    <i class="material-icons prefix">public</i>
    <select name="uf" required="true">
        <option ng-repeat="(key, value) in ufs">[{value.sigla}]</option>
    </select>
</section>

However it is not showing any value, but when inspecting the element I see the code below:

inserir a descrição da imagem aqui

He’s assigning the values to <option> of <select> but on the page for the client to select nothing appears.

Someone’s already been through it?

NOTE: I am used {[ because I switched the configs.

app.config(function($interpolateProvider) {
    $interpolateProvider.startSymbol('[{').endSymbol('}]'); 
});
  • When you give it to me: console.log(retorno.ufs) what the result?

  • Try to put ng-repeat in select

  • @Techies the result of console.log(retorno.ufs) é igual, so muda que na primeira linha fica [Object, Object....]`

  • Tried to put ng-repeat in select? @Meuchapeu

  • does not work in select

  • @Techies believe the problem is not related to the angular since the options were generated correctly.

  • @Oeslei yes, I was imagining the same, just wanted to be 100% sure

  • 1

    I particularly use ng-options no select: https://docs.angularjs.org/api/ng/directive/ngOptions

Show 3 more comments

3 answers

1

ufs is a array, not an object with keys and values.

Instead of using

<option ng-repeat="(key, value) in ufs">[{value.sigla}]</option>

Try

<option ng-repeat="value in ufs">{{value.sigla}}</option>

0

Dude, I tried to use that there

$http.get('/estados').success(function(retorno) {
    $scope.ufs = retorno.ufs;
    console.log(retorno);
});

Take a look at ngResource, an example I did on my service.

 var services = angular.module('ProcessoService', ['ngResource']);

    services.factory('ProcessosFactory', function($resource,apiUrl){
      var resource = $resource('http://restTest/retornos');

      var factory = {
          listar: function () {        
              var lista = resource.query({}, function(data){
                 lista = data;
              });
              return lista;
          }


      }


  return factory;

in controller only calls:

$scope.lista = ProcessosFactory.listar();

in html:

<tr ng-repeat="processo in processos">
            <td>{{ processo.id }}</td>
<tr>

see if it helps your case.

0

The problem is that you are using [] in the value. You’re doing it this way:

[{value.sigla}]

The correct thing would be to use {{value.sigla}}

Example: http://jsfiddle.net/81t6cbjw/25/

  • I changed the configs of {{ for {[

  • I don’t get it @Meuchapeu

  • I suspect it’s something from frameworks css o materialize, pq disabling works...

  • In my code I have: app.config(function($interpolateProvider) {&#xA; $interpolateProvider.startSymbol('[{').endSymbol('}]');&#xA;}); which allows me to use {[ instead of {{.

  • Ah yes, now I understand.

  • Try to change the order of the libraries' statements

Show 1 more comment

Browser other questions tagged

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