0

Let's say you have an *ngFor loop and you want to save a value of the current loop away into a variable in typescript. Is there a good way to do this?

Example

<tr *ngFor="let item of items | async">
    <td>{{item.one}}</td>
    <td>{{item.two}}</td>         <-- I want to save this one away -->
    <td>{{item.three}}</td>
</tr>

Typescript

this.itemCollection = this.afs.collection(CollectionIntf);
this.items= this.itemCollection.valueChanges();

So far I've been using using <iframe (load)=saveIntoVar(item.two)></iframe>. There's got to be a better way of doing this, since this iframe method doesn't always seem to work.

7
  • What is one, two, three? What does "save" mean? Commented Nov 9, 2017 at 16:17
  • those are just example names for the different properties of item. Save means to store it away into a typescript variable, as I mentioned in the first sentence. Commented Nov 9, 2017 at 16:19
  • That's fine. What does "save" mean in this context? What your code does is adding the stringified value of these properties to the DOM. Commented Nov 9, 2017 at 16:20
  • Are you asking how to get the values from an async subscription into your component controller? Commented Nov 9, 2017 at 16:20
  • Bryan- yes, I think that might be the way to put it. Commented Nov 9, 2017 at 16:21

1 Answer 1

1

There are a few good ways of getting values from an async subscription in your component controller. One is to just subscribe in the controller and assign the variable. One is to use a separate subscription in the controller, but my favorite is to just use the "do" operator:

in component:

this.items = this.itemCollection.valueChanges().do(items => console.log(items));

then you can produce whatever side effect you deem appropriate.

Sign up to request clarification or add additional context in comments.

9 Comments

I'm a bit confused by the term 'controller', but I assume it means the typescript logic. 1) Where in your solution am I specifying which item of mine I want to save away? 2) Can you just directly access a raw firebase observable in typescript? 3) What is obsSourceService? I think more code might really be useful to me. I'll add my firebase fetch above.
1. it's just a term for the typescript component class. 2. no but you can add whatever operators you want to any observable stream, one of them is "do" which will give you the value of the observable so you can... do... whatever you want with it. 3. it's just an arbitrary source for an observable I made up since I don't know what yours looks like.
updated my answer for your edits. You can't know which item in the array angular is currently rendering, nor can I imagine a situation where this would be useful information. You just use the do operator to operate on the entire array.
How can I specify which property I want to grab in the do?
you have the entire array, just iterate over it and treat it like you would any other array. items.forEach(item => console.log(item.two))
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.