2

I am completely new to Angular, and I have started directly with Angular 2. One of the things that are great about Angular is that I can modularize every feature of my web page. Components have their own html and stylesheet.

But what about their own javascript file ? How can I incluse its own particular javascript file ? I have seen this SO question is the only way available is to use SystemJS ?

For example lets say that one Component uses the Google Maps API or one component uses a charting library, how is that handled ?

2 Answers 2

1

I am using facebook sdk like below

FacebookService.ts modified from https://github.com/zyramedia/ng2-facebook-sdk. FacebookService file is partial, not pasting full file

analyze appendFacebook method to load external scripts globally but we are invoking when component requires, once external script loaded then we won't load it again as we are checking script id

@Injectable()
export class FacebookService {

    /**
     * This method is used to initialize and setup the SDK.
     * @param params
     */
    init(params: FacebookInitParams): Observable<any> {
        return new Observable(observer => {
            if (!window['fbAsyncInit']) {
                this.appendFacebook();
                window['fbAsyncInit'] = function() {
                    FB.init(params);
                    observer.next('FB initialized');
                    observer.complete();
                };
            } else {
                observer.next('FB initialized');
                observer.complete();
            }
      });

    }

    appendFacebook() {
        var js,
        id = 'facebook-jssdk',
        ref = document.getElementsByTagName('script')[0];

        if (document.getElementById(id)) {
            return;
        }

        js = document.createElement('script');
        js.id = id;
        js.async = true;
        js.src = "//connect.facebook.net/en_US/sdk.js";

        ref.parentNode.insertBefore(js, ref);
    }
}

then in component.ts file

constructor(private facebookService: FacebookService) {}

loginByFacebook() {
    let facebookParams: FacebookInitParams = {
      appId: environment.facebookAppId,
      xfbml: true,
      version: 'v2.7'
    };

    let facebookSubscription = this.facebookService.init(facebookParams).mergeMap(data => {
        return this.facebookService.login({scope: "public_profile,user_friends,email"});
    }).subscribe((response: FacebookLoginResponse) => {
      console.log(response);
    }, error => {
      console.log(error);
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

What if I don't have a .ts file ?
we can create one component or directive to load external scripts, for example <append-script src="//connect.facebook.net/en_US/sdk.js" id="facebook-jssdk"></append-script> within AppendScript component we could append script to page and handle based on id
0

In general, your app (the module) is considered to have one global set of dependencies. The way to handle this is to have all dependencies included before any of your app files.

With your example, your index.html would include the maps Javascript and the charts Javascript before it includes any component of the main application.

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.