0

I have a class with two method. I need to execute the first at the app bootstrap and the second few times during the application live.

My class:

import { Profile } from './Profile';

class Profilatura {
    public profile: Profile;

    /** @ngInject */
    first($http: angular.IHttpService): void{
      .......
      this.profile = ...;
      .......
    }

    public second(): Profile{
        return this.profile;
    }
}

In my app module:

import { ProfilaturaService } from './profilatura/ProfilaturaService';
angular.module('App')
    .run(function runBlock($http: angular.IHttpService) {
        Profilatura.first($http);
    })
    ....

Why I get Property 'first' doesn't exist on type typeof?????

1 Answer 1

1

Profilatura is a class object. You need to make first and second methods static. Beware - when you do that, you cannot use this inside the static methods anymore:

class Profilatura {
    public static profile: Profile;

    /** @ngInject */
    static first($http: angular.IHttpService): void{
      .......
      Profilatura.profile = ...;
      .......
    }

    static public second(): Profile{
        return this.profile;
    }
}

You can also make Profilatura a singleton class by doing something like this:

class Profilatura {
       private static instance: Profilatura;
       static getInstance() {
           if (!Profilatura.instance) {
               Profilatura.instance = new Profilatura();
           }
           return Profilatura.instance;
       }

        public profile: Profile;

        /** @ngInject */
       first($http: angular.IHttpService): void{
          .......
          this.profile = ...;
          .......
        }

       public second(): Profile{
            return this.profile;
        }
    }

and then use it like:

Profilatura.getInstance().first(...)
Sign up to request clarification or add additional context in comments.

3 Comments

I update the answer - when you go the static way you cannot use this inside the methods. Or you can use the singleton pattern.
I tried with the singleton pattern, but I get Property 'getInstance' doesn't exist on type typeof
Yes that was the problem. I used only one method

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.