Hiero
Hiero

Reputation: 2205

Inject factory in Angular 1.x.x ES6

I'm using Angular 1.x.x and ES6 syntax.

I have a controller like this:

    class BuilderController {  

        constructor(Auth) {
            this.foo = 'bar';
        }    

        create() {
            console.log(Auth); //this is undefined    
        }       
    }

    angular.module('myapp')
        .controller('BuilderCtrl', BuilderController);

I'm trying to inject the Auth factory into my controller but if I console log Auth in my create() method it is undefined.

Can someone please explain to me how to proprely inject factory in a Angular Controller (class)?

Upvotes: 0

Views: 273

Answers (1)

Jai
Jai

Reputation: 74738

You should use it as global property with keyword this:

class BuilderController {  

    constructor(Auth = "default") {
        this.foo = 'bar';
        this.Auth = Auth;
    }    

    create() {
        console.log(this.Auth); //if Auth is undefined then "default" gets logged.    
    }       
}

Upvotes: 2

Related Questions