0

angular 2 async observable @Input

In my component i have an property input that i want to feel when observable return value.

getAppointments(askForAnswerId: number): Observable<Appointment[]> {
return this.appointmentService.GetByAskForAnswerId(askForAnswerId);
}

<app-appointment [appointments]="
(getAppointments(askForAnswer.askForAnswerId) | async)"></app-appointment>

is it possible to do something like that ?

DashboardService

import { Injectable, OnInit } from '@angular/core';
import { AskForAnswerService } from "app/services/ask-for-answer.service";
import { Observable } from "rxjs/Observable";
import { AuthenticationService } from "app/services/auth/auth.service";
import { ApplicationUser } from "app/model/account/application-user";
import { AskForAnswer } from "app/model/ask-for-answer";
import { AppointmentService } from "app/services/appointment.service";
import { Appointment } from "app/model/appointment";

@Injectable()
export class DashboardService {

  constructor(
    private userService: AuthenticationService,
    private askForAnswerService: AskForAnswerService,
    private appointmentService: AppointmentService
  ) { }

  getUser(): Observable<ApplicationUser> {
    return this.userService.getUser();
  }

  getActiveAskForAnswer(email: string): Observable<AskForAnswer[]> {
    return this.askForAnswerService.GetActiveByEmail(email);
  }

  getAppointments(askForAnswerId: number): Observable<Appointment[]> {
    return this.appointmentService.GetByAskForAnswerId(askForAnswerId);
  }


}

DashboardComponent

import { Component, OnInit } from '@angular/core';
import { USER_NAV } from "app/model/forms/nav-item";
import { DashboardService } from "app/modules/user/dashboard/dashboard.service";
import { Observable } from "rxjs/Observable";
import { ApplicationUser } from "app/model/account/application-user";
import { AskForAnswer } from "app/model/ask-for-answer";
import { MdSnackBar } from "@angular/material";
import { DashboardServiceProvider } from "app/modules/user/dashboard/dashboard.service.providers";
import { Appointment } from "app/model/appointment";

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.scss'],
  providers: [DashboardServiceProvider]
})
export class DashboardComponent implements OnInit {

  links = USER_NAV;
  user$: Observable<ApplicationUser>;
  askForAnswersActive$: Observable<AskForAnswer[]>;
  count: number = 0;

  constructor(
    private dashboardService: DashboardService,
    public snackBar: MdSnackBar) {
    this.user$ = this.dashboardService.getUser();
    this.user$.subscribe(
      res => this.userSubscribers(res),
      errors => console.error(JSON.stringify(errors))
    );

  }

  ngOnInit() {


  }

  private userSubscribers(user: ApplicationUser): void {
    if (user) {
      this.askForAnswersActive$ = this.dashboardService.getActiveAskForAnswer(user.email);
      this.snackBar.open("Bienvenu " + user.familyName + " " + user.givenName, null, { duration: 3000 });
    }
  }

  private getAppointments(askForAnswerId: number): Observable<Appointment[]> {
    return this.dashboardService.getAppointments(askForAnswerId);
  }





}

Dashboard template

<div *ngFor="let askForAnswer of askForAnswersActive$ | async">

    <app-appointment [appointments]="(getAppointments(askForAnswer.askForAnswerId) | async)"></app-appointment>


</div>

AppointementTemplate

<div *ngFor="let appointment of appointments">
  <span>{{appointment | json}}</span>

</div>
14
  • yes, it's possible, what is your problem? did you try to implement it? Commented Apr 10, 2017 at 13:44
  • this code throw infinite loop, i don't understand why Commented Apr 10, 2017 at 13:44
  • 1
    Try this.appointments = this.appointmentService.GetByAskForAnswer(); and in template [appointments]="appointments" Commented Apr 10, 2017 at 13:48
  • 1
    appointments$: Map<number, Observable<Appointment[]>>; asks.forEach(ask => this.appointments$.set(ask.askForAnswerId, this.getAppointments(ask.askForAnswerId))); <app-appointment [appointments]="(appointments$.get(askForAnswer.askForAnswerId) | async)"></app-appointment> Commented Apr 11, 2017 at 8:01
  • 1
    @Maximus thank you for your time i m going to read it Commented Apr 11, 2017 at 8:02

2 Answers 2

1

Try

this.appointments = this.appointmentService.GetByAskForAnswer();

and in template

[appointments]="appointments" 
Sign up to request clarification or add additional context in comments.

Comments

1

@RemyaJ is rigth, the problem come from detectchange processing of angular , to avoid that i do this and it works fine :) :).

don't call function in template with async this result infinite loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.