ng-metadata icon indicating copy to clipboard operation
ng-metadata copied to clipboard

$inject in strictdi with ngMetadata services

Open rubenCodeforges opened this issue 7 years ago • 3 comments

First for all thanks for the hard work , unfortunate i didnt found any mentions in the docs or closed issues.

So the question is very simple:

someOldController.$inject = [] // this is required in stricDi
someOldController(UserServiceCreatedWithNgMetadata) {
}

the question is ,d how do i provide the ngMetadata created service in $inject ? Ive tried with string 'UserServiceCreatedWithNgMetadata' not found , ive tried with UserServiceCreatedWithNgMetadata not found , ive tried with UserServiceCreatedWithNgMetadata.name same story.

another example is httpInterceptor

@Injectable()
export class AuthInterceptor implements IHttpInterceptor {
    static factory(auth: AuthService): RequestInterceptor {
        return new AuthInterceptor(auth);
    }
    
    constructor(auth: AuthService) {
        console.log(auth);
    }

    response(response: any): any {
        console.log(response);
        return response;
    }
}



httpConfigFactory.$inject = ['$httpProvider'];
export function httpConfigFactory($httpProvider: any): any {
    $httpProvider.interceptors.push(AuthInterceptor.factory);
}

In the interceptor example i will get a strictDi error aswell

rubenCodeforges avatar May 22 '18 09:05 rubenCodeforges

even more , if i try to inject via class propperty : @Inject(AuthService) getAuth: AuthService;

i will get an error

VM88192:64 Uncaught TypeError: Cannot read property '$inject' of undefined
    at InjectMetadata.paramDecoratorForNonConstructor 

rubenCodeforges avatar May 22 '18 09:05 rubenCodeforges

If I remember correctly, you'd want to do something like $inject = [getInjectableName(AuthService)].

aciccarello avatar May 25 '18 03:05 aciccarello

@aciccarello Yep that works , thanks. That should be somewhere in the docs , because sometimes you cant take a huge app and just rewrite it all , in terms of backwards compatibility you are forced to do it in progression.

rubenCodeforges avatar May 25 '18 10:05 rubenCodeforges