0
export class ProfilePage {
chartOptions: any;
learningStyleScores: FirebaseListObservable<any>;
constructor(public navCtrl: NavController, public navParams: NavParams, af: AngularFireDatabase) {

    this.learningStyleScores.subscribe(list => {
       var Auditory = list["0"].Audio;
       var Visual = list["0"].Visual;
       var Verbal = list["0"].Verbal;
       var Logical = list["0"].Logical;
       var Solitary = list["0"].Solitary;
       var Social = list["0"].Social;
       var Physical = list["0"].Physical; 
       console.log(list)
      })
    this.chartOptions = {
       .....,
        series: [{
            name: 'Score:',
            data: [
                ['Visual', Visual],
                 ['Auditory', Auditory],
                ['Verbal', Verbal],
                ['Logical', Logical],
                ['Solitary', Solitary],
                ['Social', Social],
                ['Physical', Physical], 
            ]
        }]
    }
}

}

I want to get the value of the each of the variable and pass their on other "this" and use its value to present my data in a chart. I'm confused on how to declare a variable globally so that it will be accessible by all. What am I doing wrong?

AngularNewbie
  • 133
  • 1
  • 1
  • 11
  • Please correct the grammar for: "and pass their on other 'this' " – terpinmd Aug 03 '17 at 19:03
  • Update your question with your class code. The ptoblem with the given code is not clear yet – FAISAL Aug 03 '17 at 19:03
  • Possible duplicate of [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Heretic Monkey Aug 03 '17 at 19:06
  • Error is that from the "series", it cannot find the variable name of the declared variables – AngularNewbie Aug 03 '17 at 19:06
  • And/or [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/q/23667086/215552) – Heretic Monkey Aug 03 '17 at 19:08
  • @angularnewbie, if i understood correctly, your problem is that you want to reuse the data you get in learningStyleScores.subscribe() in other methods of the class as well? – FAISAL Aug 03 '17 at 19:14
  • @Faisal yes. since the data i got from the firebase will be used to displayed in the chart – AngularNewbie Aug 03 '17 at 19:15

1 Answers1

0

Do the following changes:

 export class ProfilePage {
    chartOptions: any;
    learningStyleScores:     FirebaseListObservable<any>;

    auditory: any;
    visual: any;
    verbal: any;
    logical: any;
    solitary: any;
    social: any;
    physical: any;

    constructor(public navCtrl: NavController, public navParams: NavParams, af: AngularFireDatabase) {

        this.learningStyleScores.subscribe(list => {
           this.auditory = list["0"].Audio;
           this.visual = list["0"].Visual;
           this.verbal = list["0"].Verbal;
           this.logical = list["0"].Logical;
           this.solitary = list["0"].Solitary;
           this.social = list["0"].Social;
           this.physical = list["0"].Physical; 
           console.log(list)

          this.chartOptions = {
             .....,
            series: [{
                name: 'Score:',
                data: [
                    ['Visual', this.visual],
                     ['Auditory', this.auditory],
                    ['Verbal', this.verbal],
                    ['Logical', this.logical],
                    ['Solitary', this.solitary],
                    ['Social', this.social],
                    ['Physical', this.physical], 
                ]
            }]
        }
     });
    }
}
FAISAL
  • 33,618
  • 10
  • 97
  • 105