#Variable assigned inside subscribe is undefined outside subscribe

8 messages · Page 1 of 1 (latest)

fast quarry
#

I am assigning a variable inside a subscribe of one of my observables although when I call the variable outside the subscribe it is returning undefined

signal sequoia
#

g!codeblock @fast quarry Don't do that. Use the AyncPipe to get values out of an Observable in Angular. Also show code for better advice

fast quarry
#

The reason behind doing it in typescript is because I am using a form builder and need one of the input values to be a specific string but formcontrolname wont let me input a value

signal sequoia
#

show code

torn escarpBOT
#

@fast quarry, you can use the following snippet to have your code formatted and colored by Discord. Replace ts with the language you need (i.e. html, js, css, etc)
```ts
// your code goes here
```

fast quarry
#

Observable subscribe:

test.subscribe(element => {
      return element.test;
    })

HTML:

<form [formGroup]="testForm" (ngSubmit)="onSubmit()">
                <fieldset class="uk-fieldset">

                    <div *ngFor = "let t of test | async" class="uk-margin">
                        <input id="test"
                        formControlName="test"
                        class="form-control" 
                        [value]="t.test"
                        type="text" 
                        placeholder="test" 
                        aria-label="Input">
                    </div>
                </fieldset>
              </form>

signal sequoia
#
  1. You cannot return from a Subscription callback.
  2. You shouldn't mix [value]= with Angular Forms.
  3. Without the context of your code I cannot help.
#

Show more code if you want help. The whole component, or at least every part relevant to this <form>