-3

I am reading data from a text file, and the reading of data works fine, and I can even console.log the data. But I am confused on how I can transfer the data into a variable. Here is my code currently:

let mazeData; //need to get this variable to equal to the 'data' in the code below

    this.httpClient.get('assets/Maze1.txt', { responseType: 'text' })
      .subscribe(data => console.log(data));
ISHAAN PATEL
  • 111
  • 1
  • 1
  • 10
  • 1
    `.subscribe(data => mazeData = data)` Now you'll probably say it doesn't work because when you log `mazeData` you get nothing in the console. And then you'll face the arduous task of wrapping your head around asynchronous execution. – Robby Cornelissen Jun 23 '20 at 03:33
  • Does this answer your question? [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Robby Cornelissen Jun 23 '20 at 03:41

1 Answers1

0

All you need to do is

this.httpClient.get('assets/Maze1.txt', { responseType: 'text' })
      .subscribe(data => mazeData = data);

I suggest you read up more on subscribe. This answer might explain things more simply to you: what is .subscribe in angular?

BatshevaRich
  • 550
  • 7
  • 19