Olá. Fiz o curso "Angular: controle e fluxo de navegação" e ao final do projeto, duas coisas não funcionaram.
1° o preview que mostar a imagem do gato não aprece quando a imagem é carregada e o percentual de carregamento também não. A imagem é carregada e vai para lista normalmente.
2° No botão de cancelar, ele deveria voltar para a tela de lista de animais, mas não vai. Utilizei o routerLink="['animais']", mas não deu certo.
Abaixo seguem os códigos do template e do componente. Se alguém puder dar uma ajuda fico grato.
<div class="container">
<form [formGroup]="formularioAnimal" class="row" (submit)="upload()">
<div class="col-md-6 text-center" >
<div class="form-group" *ngIf="!preview; else previewImage">
<button type="button" (click)="fileInput.click()" class="btn btn-primary">
<i class="fa fa-image fa-4x align-middle"></i>
</button>
<input #fileInput type="file" hidden formControlName="file" accept="image/*" (change)="gravaEvento($event.target)"/>
<app-mensagem mensagem="Por favor selecione a foto" *ngIf="!!formularioAnimal.get('file')?.errors?.required"></app-mensagem>
</div>
<ng-template #previewImage>
<app-animal [url]="preview" title="Preview"></app-animal>
</ng-template>
</div>
<div class="col-md-6">
<div class="form-group">
<textarea formControlName="description" class="form-control form-control-sm"
placeholder="Descrição do animal"
></textarea>
<app-mensagem mensagem="Tamanho máximo do campo é 300 caracteres"
*ngIf="!!formularioAnimal.get('description')?.errors?.maxlength">
</app-mensagem>
</div>
<div class="form-group">
<label class="text-muted">
Permite comentario
<input type="checkbox" formControlName="allowComments">
</label>
</div>
<div *ngIf="!percentualConcluido; else uploaddiv">
<button [disabled]="formularioAnimal.invalid" type="submit" class="btn btn-primary btn-block"
>Enviar</button>
<a routerLink="['animais']" class="btn btn-secondary btn-block">
Cancelar
</a>
</div>
<ng-template #uploaddiv>
<div class="text-center display-4">
Percentual de upload {{percentualConcluido}} %</div>
</ng-template>
</div>
</form>
</div>
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { AnimaisService } from '../animais.service';
import { Router } from '@angular/router';
import { finalize } from 'rxjs/operators';
import { HttpEvent, HttpEventType } from '@angular/common/http';
@Component({
selector: 'app-novo-animal',
templateUrl: './novo-animal.component.html',
styleUrls: ['./novo-animal.component.css']
})
export class NovoAnimalComponent implements OnInit {
formularioAnimal!: FormGroup;
file!: File;
preview!: string;
percentualConcluido = 0;
constructor(
private animaisService:AnimaisService,
private fomBuilder: FormBuilder,
private router: Router
) { }
ngOnInit(): void {
this.formularioAnimal = this.fomBuilder.group({
file: ['',Validators.required],
description: ['', Validators.maxLength(300)],
allowComments: [true],
})
}
upload(){
const allowComments = this.formularioAnimal.get('allowComments')?.value ?? false;
const description = this.formularioAnimal.get('description')?.value ?? '';
this.animaisService.upload(description,allowComments,this.file)
.pipe(finalize(() => this.router.navigate(['animais']))
).subscribe((event:HttpEvent<any>) =>{
if(event.type === HttpEventType.UploadProgress){
const total = event.total ?? 1;
this.percentualConcluido = Math.round(100*(event.loaded / total));
}
}, (error)=> console.log(error)
);
}
gravaEvento(arquivo: any): void{
const [file] = arquivo?.files;
this.file = file;
const reader = new FileReader();
reader.onload = (event:any)=>(this.preview=event.target.result);
reader.readAsDataURL(file);
}
}