admin管理员组

文章数量:1023738

I have angular 2 application (angular cli is used). And I want to build web and mobile (cordova) version for the app.

So I want to use ng build -e prod to build for production and ng build -e cordova --output-path mobile/www --base-href ./ to build for cordova project.

I want to include <script type="text/javascript" src="cordova.js"></script> if environment is cordova and exclude facebook web api script, and vise versa if environment is production

I have angular 2 application (angular cli is used). And I want to build web and mobile (cordova) version for the app.

So I want to use ng build -e prod to build for production and ng build -e cordova --output-path mobile/www --base-href ./ to build for cordova project.

I want to include <script type="text/javascript" src="cordova.js"></script> if environment is cordova and exclude facebook web api script, and vise versa if environment is production

Share Improve this question asked Dec 21, 2016 at 17:11 GlebGleb 1,3323 gold badges18 silver badges36 bronze badges 2
  • What solution did you go with? – OneMuppet Commented Mar 11, 2017 at 7:24
  • Does it have to be in it's own script tag? If not you can use require('path/to/file.js) based on env – Ahmed Musallam Commented Apr 8, 2017 at 13:50
Add a ment  | 

1 Answer 1

Reset to default 6

Found a solution.

Based on the answer by Ionaru in this issue

In main.ts:

if (environment.production) {

  document.write(
    `
      <script type="text/javascript">
         // JS code here
      </script>
    `
  );

  enableProdMode();
}

I believe this to be the simplest way to conditionally embed code as once was done directly on index.html

I have angular 2 application (angular cli is used). And I want to build web and mobile (cordova) version for the app.

So I want to use ng build -e prod to build for production and ng build -e cordova --output-path mobile/www --base-href ./ to build for cordova project.

I want to include <script type="text/javascript" src="cordova.js"></script> if environment is cordova and exclude facebook web api script, and vise versa if environment is production

I have angular 2 application (angular cli is used). And I want to build web and mobile (cordova) version for the app.

So I want to use ng build -e prod to build for production and ng build -e cordova --output-path mobile/www --base-href ./ to build for cordova project.

I want to include <script type="text/javascript" src="cordova.js"></script> if environment is cordova and exclude facebook web api script, and vise versa if environment is production

Share Improve this question asked Dec 21, 2016 at 17:11 GlebGleb 1,3323 gold badges18 silver badges36 bronze badges 2
  • What solution did you go with? – OneMuppet Commented Mar 11, 2017 at 7:24
  • Does it have to be in it's own script tag? If not you can use require('path/to/file.js) based on env – Ahmed Musallam Commented Apr 8, 2017 at 13:50
Add a ment  | 

1 Answer 1

Reset to default 6

Found a solution.

Based on the answer by Ionaru in this issue

In main.ts:

if (environment.production) {

  document.write(
    `
      <script type="text/javascript">
         // JS code here
      </script>
    `
  );

  enableProdMode();
}

I believe this to be the simplest way to conditionally embed code as once was done directly on index.html

本文标签: javascriptangular cli includeexclude scripts in indexhtml depending on the environmentStack Overflow