How to work with Array in Angular with screen printing?

1

I still know little Angular and I know only the basics, look at the code snippet:

 listarTodas(): Promise<any> {
    return this.http.get(this.cidadesUrl)
      .toPromise()
      .then(response => console.log(response.json()));
  }

See how you're doing!

  • I'd like to know how to only print attribute values Script .

  • I'd also like to know how to only print values that the Script is equal to 1 .

I'm just asking for help here because I've made several unsuccessful attempts, I've already looked in the documentation of Angular on the internet in blogs and forums and I did not find it, I need a lot of help!

    
asked by anonymous 05.02.2018 / 09:57

1 answer

2

You can use *ngForOf to print the attributes:

<!-- Assumindo que o seu controlador tem uma variavel 'cidades' onde guarda o resultado da sua promessa -->
<ul>
    <li *ngFor="let cidade of cidades">
        <span>{{cidade.nome}} => {{cidade.codigoEstado}}</span> 
        <!-- resultados -->
        <!-- Rio Branco => 1 -->
        <!-- ... -->
        <!-- Santos => 6 -->
    </li>
</ul>

The *ngFor directive will go through your array and print all the elements that are contained in it. If you add or remove elements of this array , DOM will be automatically updated.

To print only elements whose codigoEstado === 1 , you can use *ngIf :

<ul>
    <li *ngFor="let cidade of cidades">
        <span *ngIf="cidade.codigoEstado === 1">{{cidade.nome}} => {{cidade.codigoEstado}}</span> 
        <!-- resultados -->
        <!-- Rio Branco => 1 -->
        <!-- Cruzeiro Sul => 1 -->
    </li>
</ul>

As the name implies, the *ngIf directive lets you control what elements are written to the DOM. If the condition of the directive is true, the parent element is written in the DOM. If it is false, it is removed from the DOM.

    
05.02.2018 / 10:27