Hello,

I'm implementing two independent components (angular 6). One component 
should notify another one using event. Each component is responsible for 
loading it's data internally, so no shared data service is allowed. I don't 
need any UI caching, this means when something happens inside 
NewItemComponent then ListComponent should completely refresh it's data 
using back-end call.

At the moment I have following code:

export class NewItemComponent implements OnInit {

  constructor(private itemService: ItemService, private notificationService: 
NotificationService) {
  }

  ngOnInit() {
  }

  createNewItem(item: Item) {
    this.itemService.persist(item).subscribe((response: Item) => {
      console.log(response);
      this.notificationService.notifyNewItemHasBeenCreated(response);
    });
  }}

export class Component1 implements OnInit {

  items: Item[];

  constructor(private listService: ListService, private notificationService: 
NotificationService) {
  }

  ngOnInit() {
    this.loadItems();

    this.notificationService.item$.subscribe((item) => {
      if (item != null) {
        this.loadItems();
      }
    })
  }

  loadItems(){
    this.istService.getItems().subscribe((data: Item[]) => {
      this.items= data;
      console.log(this.items);
    });
  }}

@Injectable({
  providedIn: 'root'})export class NotificationService {

  private _item: BehaviorSubject<Item> = new BehaviorSubject<Item>(null);
  public  item$ = this._item.asObservable();

  constructor() {
  }

  notifyNewItemHasBeenCreated(item: Item) {
    this._item.next(item);
  }}



export class ListService {

  basePath = 'my-api.com';
  apiPath = "item";

  constructor(private httpClient: HttpClient) {
  }

  getItems(): Observable<Item[]> {
    return this.httpClient.get<Item[]>(this.basePath + '/' + this.apiPath);
  }}


What makes me very worried is that I'm calling subscribe() each time is 
loadItems() is called. Unfortunately I can't find any information about my 
scenario in Angular Tutorials / Documentation.

Can you please help me with it? 

Thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to