0

i am new to Angular so i think i'm missing something.

i have 3 variables in my component, those variables are being filled after calling .subscribe method on an observable object.

like this

this.interRetard = this.technicienService.getInterRetard(id).subscribe(data => {
  this.interRetard = data;
});

the interRetard variable contains a number, i can display its value in the html by doing {{interRetard}} .

the problem is that when i try to display it with console.log() it says :

Subscriber {closed: false, _parent: null, _parents: null, _subscriptions: Array(1), syncErrorValue: null, …}

and i need it to give me the exact value like it is displaying it in the html so i can assign it to the chart like this

public doughnutChartData:number[] = [this.interRetard ,  5, 10];

Thank you.

5
  • 1
    You seem to be using this.interRetard 2 times. Once to hold the subscription returned by the subscribe, and again when you assign data to it inside the subscribe. Commented Nov 21, 2018 at 18:34
  • 1
    this.interRetard is being assigned twice in your statement. You only need to assign it inside the subscribe callback. Commented Nov 21, 2018 at 18:34
  • i removed it and something weird is happepning :/ when i do console.log(interRetard) it displayes undefined then when i trigger the method again (selection changed of a select box) it shows the right value :/ :/ Commented Nov 21, 2018 at 18:41
  • What is the type of your interRetard ? Commented Nov 21, 2018 at 18:43
  • the type is any .. i have bad news also this is not working doughnutChartData:number[] = [this.interRetard ,this.interFinie,this.interAvance]; Commented Nov 21, 2018 at 18:46

1 Answer 1

1

This should work:

   this.technicienService.getInterRetard(id).subscribe(data => {
      this.interRetard = data;
      console.log(this.iterRetard);
      this.myFunction(this.iterRetard) or this.myFunction(data);
    });

myFunction(p) {
// Your Logic //
}
Sign up to request clarification or add additional context in comments.

5 Comments

yes i know this works .. but i need the variable outside the method subscribe so i can put it in an array and assign the array to a chart like this ... public doughnutChartData:number[] = [this.interRetard ,this.interFinie,this.interAvance];
can you please explain your last comment .. i don't seem to get what u're saying
I edited the answer, let me know if that answers your question!
@kmarakrout: that comment was partial, so removed it!
no problem .. i think your answer is working .. i'm trying something if it works perfectly i'll let you know .. thanks

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.