nobinobiru
nobinobiru

Reputation: 792

angular.js doesn't work ng-view tag in PhoneGap

I try to use angular.js with PhoneGap.It works fine at chrome browser.But it doesn't work
at ng-view tag.And angular module doesn't called when I run it on the simulator. Do you have any idea?

My code is like this.

index.html

   <body>
        <div class="app" >
            <h1>Welcome!</h1>
            <div id="deviceready">
                            <div ng-view></div>
            </div>
        </div>

        <script type="text/javascript" src="cordova-2.0.0.js"></script>
        <script type="text/javascript" src="js/index.js"></script>


        <script type="text/javascript">
            app.initialize();
        </script>
                <script src="http:////cdnjs.cloudflare.com/ajax/libs/zepto/1.0rc1/zepto.min.js"></script>
                <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"> </script>
                <script type="text/javascript" src="js/router.js"></script>

    </body>

index.js

var app = {
    initialize: function() {
        this.bind();
    },
    bind: function() {
        document.addEventListener('deviceready', this.deviceready, false);
    },
    deviceready: function() {
        // note that this is an event handler so the scope is that of the event
        // so we need to call app.report(), and not this.report()
        app.report('deviceready');
    },
    report: function(id) { 
        console.log("report:" + id);
        // hide the .pending <p> and show the .complete <p>
        document.querySelector('#' + id + ' .pending').className += ' hide';
        var completeElem = document.querySelector('#' + id + ' .complete');
        completeElem.className = completeElem.className.split('hide').join('');
    }
};

router.js

angular.module("app",[]).
    config(["$routeProvider",function($routeProvider){
        $routeProvider.when("/",{templateUrl:"templates/home.html"});
    }]);

Upvotes: 8

Views: 5380

Answers (3)

kzar
kzar

Reputation: 3209

I just spent maybe 5 hours trying to work around a similar problem. It turned out that phone-gap / cca was for some reason prepending the app-id to the hash part of the URL. The route therefore didn't match and the user was constantly forwarded back by the $routeProvider.otherwise() call.

(I've since discovered it's a known issue with CCA that's even been fixed just not released!)

Anyway for the mean time if you're having problems getting ngRoute working with mobile-chrome-tools and Phonegap try adding two optional matching groups to your routes as a work around. For example take this example:

$routeProvider.when('/', { templateUrl: 'templates/home.html', controller: 'HomeCtrl' });
$routeProvider.when('/some-page', { templateUrl: 'templates/some-page.html', controller: 'SomePageCtrl' });
$routeProvider.when('/another-page', { templateUrl: 'templates/another-page.html', controller: 'AnotherPageCtrl' });
$routeProvider.otherwise({ redirectTo: '/' });

The routes wont match with the extra bits being added to the URL but you could work around it like so:

$routeProvider.when('/:a?/:b?/some-page', { templateUrl: 'templates/some-page.html', controller: 'SomePageCtrl' });
$routeProvider.when('/:a?/:b?/another-page', { templateUrl: 'templates/another-page.html', controller: 'AnotherPageCtrl' });
$routeProvider.when('/:a?/:b?/', { templateUrl: 'templates/home.html', controller: 'HomeCtrl' });
$routeProvider.otherwise({ redirectTo: '/' });

Notice the two optional groups I've added. Also note that the base route comes last - otherwise it will match all / most routes if the URL is generated properly!

Upvotes: 0

David
David

Reputation: 8640

Try the bootstrap api method to manually start the app on deviceReady. something like:

function onDeviceReady() {
    ...
    angular.bootstrap(document, ['ngView']);
    ...
}

document.addEventListener("deviceready", onDeviceReady, true);

http://docs.angularjs.org/api/angular.bootstrap

Upvotes: 11

mccraveiro
mccraveiro

Reputation: 55

Try downloading angularjs and zepto files from this servers and placing then within the app.

Upvotes: 0

Related Questions