2

I creating a android app in ionic 2. I declare a variable in script tag in index.html. this constant value i want to use in ionic 2 pages and providers code. i pasted my code below. thanks in advance...

index.html

    <html lang="en">
    <head>
      <title>Ionic</title>
      <meta charset="UTF-8">

      <link ios-href="build/css/app.ios.css" rel="stylesheet">
      <link md-href="build/css/app.md.css" rel="stylesheet">
      <link wp-href="build/css/app.wp.css" rel="stylesheet">  
    </head>
    <body>

      <ion-app></ion-app>

      <!-- cordova.js required for cordova apps -->
      <script src="cordova.js"></script>

      <script src="build/js/es6-shim.min.js"></script>
      <!-- Zone.js and Reflect-metadata  -->
      <script src="build/js/Reflect.js"></script>
      <script src="build/js/zone.js"></script>
      <!-- the bundle which is built from the app's source code -->
      <script src="build/js/app.bundle.js"></script>

    <script type=​"text/​javascript">​
       BASE_APP_URL="www.google.com";
    </script>​
    </body>
    </html>

item-details.ts

    import {Component} from '@angular/core';
    import {NavController, NavParams} from 'ionic-angular';
    @Component({
      templateUrl: 'build/pages/item-details/item-details.html'
    })
    export class ItemDetailsPage {
      selectedItem: any;

      constructor(public navCtrl: NavController, navParams: NavParams) {
                this.selectedItem = navParams.get('item');
                console.log(BASE_APP_URL);
         <!---it gives an error that name
          Error TS2304: Cannot find    name 'BASE_APP_URL'. -->                                 
          }
        }
1
  • The proper way to do this is this one. Commented Aug 17, 2016 at 15:37

1 Answer 1

1

Even though a better way to handle static settings in an Ionic app would be the one in this post, you can make your code work by:

  1. Including the script with your variable before the <script src="build/js/app.bundle.js"></script> because that's where you're going to use it.

  2. Declaring that variable as part of the window object implicitly.

    <!-- Your variable -->
    <script type=​"text/​javascript">​
        window.BASE_APP_URL="www.google.com";
    </script>​
    
    <!-- the bundle which is built from the app's source code -->
    <script src="build/js/app.bundle.js"></script>
    

And then in your code:

import {Component} from '@angular/core';
import {NavController, NavParams} from 'ionic-angular';
@Component({
  templateUrl: 'build/pages/item-details/item-details.html'
})
export class ItemDetailsPage {
  selectedItem: any;

  constructor(public navCtrl: NavController, navParams: NavParams) {
    this.selectedItem = navParams.get('item');
    console.log(window.BASE_APP_URL);                    
  }
}

That being said, I still recommend this approach when handling static settings in any Ionic 2 app.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for reply @sebaferreras!! i used second approch...and it works..

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.