-1

I used to call functions from the template but I learned recently that it is not a good idea since it renders time. I tried to assign value to variable and subscribe it to change using setting service observables but it does not work for me.

public getMaxUnit(){
return MaxUnits.Dollars;
}
<div style="display: flex;">
        <table style="width:100%;">
            <thead>
                <tr>
                    <th>Max</th>
                </tr>
            </thead>
            <tbody>
                <tr *ngFor="let maxValue of maxValueTable">
                    <td> {{ maxValue | numberFormatter: getMaxUnit()}}</td>
                </tr>
            </tbody>
        </table>
</div>

How can I assign please the value to a variable using .subscribe() method?

2
  • You code does not contain any Observable. Can you clarify the question?
    – Tobias S.
    Nov 20, 2022 at 4:03
  • I added the code without observables because it does not work for me Nov 20, 2022 at 4:20

1 Answer 1

1

You can use observables in your html page with async pipe. In your code, your pipe takes an observable parameter. To achieve this you need to wrap your getMaxUnit with async pipe.

Here is a similar example you can check and you can change getDateFormat function to how its date is changing.


public getMaxUnit():Observable<string>{
   return of(MaxUnits.Dollars);
}


<div style="display: flex;">
        <table style="width:100%;">
            <thead>
                <tr>
                    <th>Max</th>
                </tr>
            </thead>
            <tbody>
                <tr *ngFor="let maxValue of maxValueTable">
                    <td> {{ maxValue | numberFormatter: (getMaxUnit() | async)}}</td>
                </tr>
            </tbody>
        </table>
</div>

1
  • Thanks for your response but still calling a function from the template in this case, no? Nov 20, 2022 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.