Listing information with select

Asked

Viewed 65 times

0

I have already opened a question of this here and a user of the site helped me, and cleared my doubts. However, when trying to use the code again that he presented me, the same is presenting errors. He presented me the code with States, and I modified it to Neighborhoods (clean code because the information will be put yet.)

The code is as follows::

<script>
(function() {
  'use strict';

  angular
.module('appBairros', []);

  angular
.module('appBairros')
.controller('BairroController', BairroController);

  BairroController.$inject = [];

  function BairroController() {
var bairro = this;
bairro.opcoes = [];

iniciar();

function iniciar() {
  bairro.opcoes = [];
  bairro.opcoes.push({nome: "Botafogo", informacoes: "Botafogo é um bairro     que começa com B"});
  bairro.opcoes.push({nome: "Madureira", informacoes: "Madureira é um bairro     que começa com M"});

  bairro.seleciona = bairro.opcoes[0];
}
  }
    })();

<div ng-app="appBairros">
<div ng-controller="BairroController as bairro">
<label class="bairros">Selecione o Bairro para exibir as informações que             deseja.</label>
<select ng-options="opcao.nome for opcao in bairro.opcoes" ng-model="bairo.selecionado"></select>

<br>
<br>

{{bairro.selecionado.informacoes}}

He should present like this: inserir a descrição da imagem aqui

And when I run my code, it presents that way:

inserir a descrição da imagem aqui

2 answers

1

your error is within the function iniciar() in bairro.seleciona = bairro.opcoes[0]; must be bairro.selecionado = bairro.opcoes[0]; and also change ng-model="bairo.selecionado" for ng-model="bairro.selecionado" being bairro and not bairo

(function() {
  'use strict';

  angular
  .module('appBairros', []);

    angular
  .module('appBairros')
  .controller('BairroController', BairroController);

  BairroController.$inject = [];

  function BairroController() {
    var bairro = this;
    bairro.opcoes = [];

    iniciar();

    function iniciar() {
      bairro.opcoes = [];
      bairro.opcoes.push({nome: "Botafogo", informacoes: "Botafogo é um bairro     que começa com B"});
      bairro.opcoes.push({nome: "Madureira", informacoes: "Madureira é um bairro     que começa com M"});

      bairro.seleciona = bairro.opcoes[0];
    }
  }
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>

<div ng-app="appBairros">
<div ng-controller="BairroController as bairro">
<label class="bairros">Selecione o Bairro para exibir as informações que             deseja.</label>
<select ng-options="opcao.nome for opcao in bairro.opcoes" ng-model="bairro.selecionado"></select>

<br>
<br>

{{bairro.selecionado.informacoes}}

  • I made this correction and has not changed in the execution. It continues running without presenting the options.

  • check your typing, in your model, Voce is using bairo with 1 R .. and should use neighborhood with 2 R

1

Two small typing errors.

  • In the controller, where it reads bairro.seleciona = bairro.opcoes[0]; should be read bairro.selecionado = bairro.opcoes[0];
  • In the view, where it reads ng-model="bairo.selecionado" should be read ng-model="bairro.selecionado"

With these two fixes the code works, as can be seen in the version below:

angular.module('appBairros', []);

angular.module('appBairros').controller('BairroController', BairroController);

BairroController.$inject = [];

function BairroController() {
var bairro = this;
bairro.opcoes = [];

iniciar();

function iniciar() {
bairro.opcoes = [];
bairro.opcoes.push({nome: "Botafogo", informacoes: "Botafogo é um bairro     que começa com B"});
bairro.opcoes.push({nome: "Madureira", informacoes: "Madureira é um bairro     que começa com M"});

bairro.selecionado = bairro.opcoes[0];
}
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="appBairros">
<div ng-controller="BairroController as bairro">
<label class="bairros">Selecione o Bairro para exibir as informações que             deseja.</label>
<select ng-options="opcao.nome for opcao in bairro.opcoes" ng-model="bairro.selecionado"></select>

<br>
<br>

{{bairro.selecionado.informacoes}}

  • I fixed the observed errors and still when I run the options do not appear

  • @Inezboldrin the code works when you click the Run button above?

  • It does work. It works the way it’s supposed to work for me, too. Inclusive already got the code that is fixed and replaces what is in my file and still does not present the options.

  • @Inezboldrin then your error may be being caused by some other aspect of your application. The console shows some error?

  • Just these two: Uncaught Referenceerror: angular is not defined at especialidade.html:107 at especialidade.html:130

  • Ah, it seems to me that you are not loading the Angular library. Include the following line at the beginning of your HTML : <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> (or the version you want.)

  • I have this library here archive: <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"> </script>. Replace this one with the one you showed me, and still nothing.

  • 1

    The URL you are using in your code includes the character &zwnj; (zero-width non-joiner). Replace the entire original line with the one I sent.

Show 3 more comments

Browser other questions tagged

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