1

I'm using Angular 18 with signals. My CalculationService basically calculates a price based on some parameters. Building with ng serve doesn't throw any errors neither does VSCode, however my browser throws the following:

 ERROR TypeError: this.doorAmount is not a function at Object.calculatePricePerDoor [as computation] (calculation.service.ts:52:48)

And my component doesn't load.

calculation.service.ts

import { computed, Injectable, signal } from '@angular/core';
import {
  basePrice,
  basePricePerDay,
  basePricePerDoor,
  minutesPerDoor,
  stepPrice,
} from '../consts';

@Injectable({
  providedIn: 'root',
})
export class CalculationService {
  #price = signal(0);
  #basicDoorAmount = signal(0);
  #electroDoorAmount = signal(0);
  #automaticDoorAmount = signal(0);

  doorAmount = computed(this.countDoorAmount);
  price = computed(this.#price);
  pricePerDoor = computed(this.calculatePricePerDoor);
  priceStep = computed(this.calculatePriceStep);
  hours = computed(this.countHours);
  days = computed(this.countDays);

  constructor() {
    this.calculatePrice();
  }

  calculatePrice() {
    this.#price.set(
      basePrice +
        this.pricePerDoor() +
        basePricePerDay * this.days() +
        this.priceStep()
    );
  }

  setBasic(amount: number) {
    this.#basicDoorAmount.set(amount);
  }

  setElectro(amount: number) {
    this.#electroDoorAmount.set(amount);
  }

  setAutomatic(amount: number) {
    this.#automaticDoorAmount.set(amount);
  }

  private calculatePricePerDoor() {
    const { basic, electro, automatic } = this.doorAmount();
    return (
      basic * basePricePerDoor.basic +
      electro * basePricePerDoor.electro +
      automatic * basePricePerDoor.automatic
    );
  }

  private calculatePriceStep() {
    const { basic } = this.doorAmount();
    if (basic < 21) return 0;
    if (basic < 101) return stepPrice[100];
    if (basic < 251) return stepPrice[250];
    if (basic < 1001) return stepPrice[1000];
    return stepPrice[1001];
  }

  private countDoorAmount() {
    return {
      basic: this.#basicDoorAmount(),
      electro: this.#electroDoorAmount(),
      automatic: this.#automaticDoorAmount(),
    };
  }

  private countHours() {
    const { basic, electro, automatic } = this.doorAmount();
    return (
      (basic * minutesPerDoor.basic +
        electro * minutesPerDoor.electro +
        automatic * minutesPerDoor.automatic) /
      60
    );
  }

  private countDays() {
    return Math.ceil(this.countHours() / 7.5);
  }
}

2 Answers 2

3

This is a this context issue, you should use an arrow function to keep the context of the class.

doorAmount = computed(() => this.countDoorAmount());
Sign up to request clarification or add additional context in comments.

Comments

1

You can also leverage .bind(this) to get rid of the arrow function, if that helps.

doorAmount = computed(this.countDoorAmount.bind(this));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.