Solucionado (ver solução)
Solucionado
(ver solução)
6
respostas

Erro com o NgFor

Meu código está idêntico ao do professor no Capítulo 3 e Aula 3, quando eu uso a diretiva ngFor pela primeira vez, mas aparece o seguinte erro:

Can't bind to 'ngForOf' since it isn't a known property of 'img'. ("]ngFor="let foto of fotos" class="img-responsive center-block" src="{{foto.url}}" alt="{{foto.titulo}"): FotoComponent@0:5 Property binding ngForOf not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "directives" section. ("[ERROR ->]<img ngFor="let foto of fotos" class="img-responsive center-block" src="{{foto.url}}" alt="{{foto.ti"): FotoComponent@0:0 ; Zone: ; Task: Promise.then ; Value: Error: Template parse errors: Can't bind to 'ngForOf' since it isn't a known property of 'img'. ("]ngFor="let foto of fotos" class="img-responsive center-block" src="{{foto.url}}" alt="{{foto.titulo}"): FotoComponent@0:5 Property binding ngForOf not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "directives" section. ("[ERROR ->]<img ngFor="let foto of fotos" class="img-responsive center-block" src="{{foto.url}}" alt="{{foto.ti"): FotoComponent@0:0

Já perdi a tarde inteira e não sei o que eu faço mais rs.

6 respostas

Olá!

Compartilhe o código do seu template para que eu e outros possam ver.

Oii Fabricio!!

Pelo erro que vc compartilhou parece que esqueceu o asterisco antes da diretiva ngFor. Ela deve ser usada assim, por exemplo:

<img *ngFor="let foto of fotos" src="{{foto.url}}">

Se mesmo assim o erro continuar, por favor copie e cole seu código aqui!

abraços

Olá!

segue o código:

<img *ngFor="let foto of fotos" class="img-responsive center-block" src="{{foto.url}}" alt="{{foto.titulo}}">

app.components.ts

import { Component, Inject } from '@angular/core';
import { Http } from '@angular/http';

@Component({
    moduleId: module.id,
    selector: 'app',
    templateUrl: './app.component.html'
})
export class AppComponent {

    fotos: Object[] = [];

    constructor(http: Http) {

        http.get('v1/fotos')
            .map(res => res.json())
            .subscribe(
                fotos => this.fotos = fotos,
                erro => console.log(erro)
            );
    }
}

O detalhe é que quando eu dou o console.log pra exibir os dados buscados em this.fotos, ele busca os dados corretamente no console

foto.component.ts

import {Component, Input} from '@angular/core';

@Component({
    moduleId: module.id,
    selector: 'foto',
    templateUrl: './foto.component.html'
})
export class FotoComponent {

    @Input() titulo;
    @Input() url;
}
solução!

Oii Fabrício!!

Acho que encontrei! O *ngFor tem que estar no componente <foto> e não no template dele. O app.component.html, deve ficar assim:

 <div>
    <h1 class="text-center">AluraPic</h1>
 </div>
 <div class="container">
    <foto *ngFor="let foto of fotos" url="{{foto.url}}" titulo="{{foto.titulo}}"></foto>
 </div>

O foto.component.html deve ficar assim:

<img [src]="url" [alt]="titulo" class="img-responsive center-block">

Veja se vai! :D

Está certíssima Vanessa! Realmente cometi um vacilo e tanto! rs. Muito obrigado pela resposta. Deu certo aqui.

Muito obrigado também professor Flávio pela atenção.

Vou seguir adiante agora com o curso rsrs.

Bons estudos Fabrício!

:D

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software