Angular2, injecting constants

985 views
Skip to first unread message

Miha Zoubek

unread,
Mar 24, 2016, 3:54:03 AM3/24/16
to AngularJS
hi,

need a little help with injecting constants in Angular2:

/*models.ts*/
import {Injectable,bind,Inject} from 'angular2/core';
 
export var TEST_API_URL: string = 'https://www.googleapis.com/youtube/v3/search';
 
    export var testServiceInjectables: Array<any> = [
        bind(TEST_API_URL).toValue(TEST_API_URL)
    ];
 
/*boot.ts*/
 
import {testServiceInjectables} from './services/models';
bootstrap(loginForm, [HTTP_PROVIDERS, youTubeServiceInjectables]);
 
/*app.ts*/
 
import {Injectable, bind, Component, Inject} from 'angular2/core';
 
export class test{
constructor(public http: Http,
        @Inject(TEST_API_KEY) private apiKey: string
        ){
    }
}

But getting all the time Cannot find name 'YOUTUBE_API_KEY'".

I doing like tutorial in ng-book2 but i do not know what I am missing.
tnx
miha

Günter Zöchbauer

unread,
Mar 25, 2016, 5:20:59 AM3/25/16
to AngularJS
If you get the error 

> Cannot find name 'YOUTUBE_API_KEY'".

then probably something is expecting a `YOUTUBE_API_KEY`. This value is not used anyway in the code in your question.

You export `testServiceInjectables` but prvide `youTubeServiceInjectables`

You request `@Inject(TEST_API_KEY)` but the constant from further above is named `TEST_API_URL`

>  bind(TEST_API_URL).toValue(TEST_API_URL)

is derprecated syntax and using the value as key doesn't make sense.

It should probably be something like  `provide(TEST_API_KEY, {useValue: TEST_AIP_URL})`

Your question looks like generated by some random text generating service. I'm not able to make any sense of it
Reply all
Reply to author
Forward
0 new messages