1

I have an AngularJS app. I want this app to run both in the browser and be able to distribute it with PhoneGap. In an attempt to do this, I have the following code:

index.html

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript" src="/app/app.js"></script>
</head>

<body>
  ...
  <script type="text/javascript">
    if (window.location.protocol === "file:") {
      document.addEventListener('deviceready', startApp, false);
    } else {
      startApp();
    }
  </script>
</body>
</html>

app.js

function startApp() {
  var myApp = angular.module('myApp', ['ngRoute']);
  myApp.config(function() {
    ...
  });

  myApp.run(function() {
    ...
  });
}

controller.js

"use strict";

myApp.controller('MyController', function($rootScope, $scope, $location) {
  ...
});

myApp.controller('MyOtherController', function($rootScope, $scope, $location) {
  ...
});

...

If I add controllers.js after app.js in the head tag, I get an error that says:

Uncaught ReferenceError: myApp is not defined 

This is happening because I'm manually bootstrapping the AngularJS app. I need to manually bootstrap it because of my need to startApp. For this reason, I need a way to dynamically load controllers.js from within startApp. Yet, I'm not sure how to do that. Can anyone provide some help?

THank you

4
  • weblogs.asp.net/dwahlin/archive/2013/05/22/… Commented May 26, 2014 at 14:51
  • My challenge is, I don't want to just load controllers though. I actually need to dynamically load 2 or 3 javascript files at app start. Commented May 26, 2014 at 14:56
  • You can use the same approach to dynamically load any file you want, I've used the approach in that link to load services, directives etc. dynamically. I think if you fiddle around with the idea a bit you'll see that its possible to do what you want to do as well. Commented May 26, 2014 at 14:59
  • Can try adding the script tag at runtime Commented May 26, 2014 at 15:04

3 Answers 3

2

you have missed ng-app="" attribute in html tag...

<!DOCTYPE html>
    <html ng-app="myApp">
    <head>
      <script type="text/javascript" src="/app/app.js"></script>
    </head>

    <body>
      ...
      <script type="text/javascript">
        if (window.location.protocol === "file:") {
          document.addEventListener('deviceready', startApp, false);
        } else {
          startApp();
        }
      </script>
    </body>
    </html>
Sign up to request clarification or add additional context in comments.

Comments

0

Why don't you give requirejs a try? He has a strong dependency management based loader and will avoid issues like this.

Comments

0

First thing first: To initialize your app angular module its require angular-route.js with angular.js file and these js file should be loaded before your app js.

<head>


<script type="text/javascript" src="/app/angular.js"></script>
 <script type="text/javascript" src="/app/angular-route.js"></script>
      <script type="text/javascript" src="/app/app.js"></script>
    </head> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.