Update the view in Angular 5?

0

I'm using a method that erases the files from the database by clicking on the trash when I click it, but it does not get updated in the view if I do not do f5 in the browser as I could do this without having to update the page

import { Component,DoCheck } from '@angular/core';
import { Http } from '@angular/http'
import {HttpClient} from '@angular/common/http';
import {LoginService} from '../api/login/login.service';

@Component({
selector: 'app-main',
templateUrl: './main.component.html',
styleUrls: ['./main.component.css'],
providers:[LoginService]
})
export class MainComponent implements DoCheck {

public archivo; 

constructor(private _http: HttpClient,
public _servicio:LoginService) { 


}

borrar(archivo){


console.log(archivo);

this._servicio.borrararchivos(archivo).subscribe(data => {

}, error => {

console.log('error');

});



 }

here is my application as you see it deletes the object but it is not updated or deleted in the view

and my service is as follows

 borrararchivos(id): Observable<any>{

 const url = 'http://localhost/api/public/delete/${id}';


 let headers = new HttpHeaders().set('token',this.token);

 return this._http.delete(url,{headers:headers});

 }
    
asked by ortiga 01.03.2018 в 20:37
source

1 answer

1

According to what you show, you have a variable file in your component that is the one that is shown in the view, so this would suffice:

borrar(archivo){
  console.log(archivo);
  this._servicio.borrararchivos(archivo).subscribe(data => {
    this.archivo = null; 
// Haces null la variable de tu archivo, también puede ser {} (Objeto vació)
  }, error => {
    console.log('error');
  });

}
  

Remember that this.archivo = null; , refers to the variable file in your component, not the one that comes to you as a parameter since in the future you could have a list of elements and you would have to delete the element (called file here) from within from the component list.

Remember that Angular links your variables directly to the view and reflects their changes automatically, so whenever you modify your variables this will be reflected in the view and you do not have to do F5 .

    
answered by 02.03.2018 / 12:23
source