27

I'm having a problem with ngView in PhoneGap.

Everything seems to be loading just fine and I can even get a basic controller working using ng-controller. But when I try to use routing with ngView, nothing happens.

index.html

<!doctype html>
<html ng-app>
<head>
    <script type="text/javascript" src="lib/cordova-2.4.0.js"></script> 
    <script type="text/javascript" src="lib/angular-1.0.4.min.js"></script>
    <script type="text/javascript" src="js/app.js"></script>
</head>
<body>

<a href="#/test">Test</a>

<div ng-view></div>

</body>
</html>

app.js

angular.module('App', []).config(function ($routeProvider) {

    $routeProvider.when('/test', {
        controller: TestCtrl,
        template: '<h1> {{ test }} </h1>'        
    });

});

function TestCtrl($scope) {
    $scope.test = "Works!";
}

The Eclipse logger shows onMessage(onPageFinished, fle:///android_asset/www/index.html#/test) every time I click the link, and trying it without the # just raises an error that the path can't be found.

From what I've ready everywhere else, this should be working. Any help would be greatly appreciated.

thewildpendulum
  • 1,225
  • 1
  • 13
  • 19

5 Answers5

43

After searching through several questions and forums, I've finally got it working reliably. This is what it took me to get it running from a clean PhoneGap project.

index.html

<!DOCTYPE html>
<html ng-app="App">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <title>Hello World</title>
</head>
<body>

    <a href="#/">Main View</a>
    <a href="#/view">New View</a>

    <div ng-view></div>

    <!-- Libs -->
    <script type="text/javascript" src="lib/cordova-2.5.0.js"></script>
    <script type="text/javascript" src="lib/angular-1.0.5.js"></script>

    <!-- App -->
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript" src="js/routers.js"></script>
    <script type="text/javascript" src="js/controllers.js"></script>
    <script type="text/javascript">
        app.initialize();
    </script>
</body>
</html>

Note the <html ng-app="App"> tag. The app won't load without a value for the directive, so make sure you include one.

index.js

var app = {
    initialize: function() {
        this.bindEvents();
    },
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, true);
    },

    onDeviceReady: function() {
        angular.element(document).ready(function() {
            angular.bootstrap(document);
        });
    },
};

In this file, we're manually bootstrapping Angular when PhoneGap fires the 'ondeviceready' event.

routers.js

angular.module('App', [])
.config(function ($compileProvider){
    $compileProvider.urlSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|tel):/);
})
.config(function ($routeProvider) {

    $routeProvider
    .when('/', {
        controller: TestCtrl,
        templateUrl: 'partials/main.html'
    })
    .when('/view', {
        controller: ViewCtrl,
        templateUrl: 'partials/view.html'
    });
});

This file has two important things in it. First, we're creating the module with the same name we used before in <html np-app="App">. Second, we need to configure the router to whitelist file URIs. I personally didn't need this, but several people seem to have encountered the issue, so I included it.

controllers.js

function TestCtrl($scope) {
    $scope.status = "It works!";
}

function ViewCtrl($scope) {
    $scope.status = "Also totally works!";
}

Finally, just some basic controllers.

I've created a github repo with all of this here.

Hope this helps someone else.

thewildpendulum
  • 1,225
  • 1
  • 13
  • 19
  • 18
    In case anyone else runs into this, urlSanitizationWhitelist is aHrefSanitizationWhitelist in newer versions of Angular – Ben McCann Sep 11 '13 at 06:07
  • 1
    Here we are a year later: I notice that bootstrapping angular this way in Cordova yields this error: '''Error: ng:btstrpd App Already Bootstrapped with this Element 'document' ''' --indicating that angular is already loaded. – ericpeters0n Apr 28 '14 at 23:44
  • I ran into a similar error. The solution was not to add the "ng-app" attribute to the HTML if you're calling angular.bootstrap. You can do one or the other, but not both. – hawkharris Jun 17 '14 at 18:26
  • Is it possible to pass the module to a variable like var appModule = angular.module('App',[]) etc? – Mr A Sep 26 '14 at 08:50
  • And where would you initialize a webSql database? – Dario M. Mar 02 '15 at 18:16
  • @thewildpendulum Any idea on - http://stackoverflow.com/questions/38071421/cordova-ios-app-on-device-is-not-running – Smitha Jun 29 '16 at 03:20
5

My issue was the domain whitelisting.

Basically your .config needs to look like this:

angular.module('App', []).config(function ($routeProvider, $compileProvider) {

    $routeProvider.when('/test', {
        controller: TestCtrl,
        template: '<h1> {{ test }} </h1>'        
    });
    $compileProvider.urlSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|tel):/);
});
forrestranger
  • 111
  • 1
  • 9
2

FWIW - here's my 2 cents for this problem:

All of the above seemed to work and promote me towards a working app but I still couldn't get the $routeprovider to navigate between pages...

MY FINAL problem was - I had an include inside a script tag of the jqueryMobile library and it was hijacking the URL requests before Angular got hold of them. When I removed it the navigation requests finally made it to the $routeprovider and all is working fine now.

Hope this helps someone...

roy650
  • 564
  • 1
  • 7
  • 17
1

I was able to resolve the issue by disabling the local access policy flags in Google Chrome.

$ open -a Google\ Chrome --args --disable-web-security for Mac OS.

Here is the link to how to disable local access file policy.

Community
  • 1
  • 1
art-jitsu
  • 21
  • 1
0

Angular 1.2 does not provide the method

$compileProvider.urlSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|tel):/)

Anyway, it seems like it's not necessary anymore. I tested it on my mobile and it works without.

akohout
  • 1,742
  • 3
  • 21
  • 41