Because when changing components, multiply the objects of * ngForm?

0

Hello, how are you? I'm creating a test project to learn how to handle angle 6 since I come from angularjs and I'm having a problem with the ngFof or with the variable that the ngFor traverses, I'm not sure but when I move from one component to another and then I go back to previous component the ngFor items are duplicated. The flow is as follows: - The Home page makes a get and with the ngFor it lists the data of my bd. - Then when entering an individual data I pass the id of that data to obtain information about this specific data by going to the project component. - When returning to the previous component the ngFor data is duplicated.

step gif here , this is the code that I have in the home.component.ts, the truth is that I'm not realizing that it could be

import { Component, OnInit } from '@angular/core';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { ProjectService } from '../../../service/project.service';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})

export class HomeComponent implements OnInit {
  closeResult: string;
  projects = [];
  project = {};
  constructor(private modalService: NgbModal, private projectService: ProjectService){ 
    this.projects = [];
  }

  ngOnInit() {
  	this.projects = this.projectService.get();
  }
  openLg(content) {
    this.modalService.open(content, { size: 'lg' });
  }
  modalProject(): Observable<any> {
    let cb = Function;
  this.projectService.add(this.project, callback=>{
    this.projects.push(callback);
  });
  this.project = {};
  
  return;
  }

}

Thanks greetings!

    
asked by Nicolas Machado 28.10.2018 в 23:08
source

1 answer

1

Welcome. It would have been very positive for the resolution of your problem that you share the code of the service that you are using to make the request to the DB.

Remember that Angular services are intended to share information between components, which means that if you have a declared arrangement in your service that you push every time you enter your component home Information will be kept in cache even if the OnDestroy method of home is launched. This could be the reason why every time you enter the home component the data is duplicated. If this does not solve your problem, share the code of the service you use.

    
answered by 28.10.2018 в 23:26