简体   繁体   中英

Why won't my AngularJS view load in Cordova?

My index.html is:

<!DOCTYPE html>
<html>
  <head>
    <meta 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" />
    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <title>Hello World</title>
  </head>
  <body ng-app="app">
    THIS WORKS NOW!
    <div ng-view></div>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/angular.min.js"></script>
    <script type="text/javascript" src="js/angular-route.min.js"></script>
    <script type="text/javascript" src="js/app.js"></script>
  </body>
</html>

And in app.js , I have:

var app = angular.module('app', ['ngRoute']);

app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
    $routeProvider.when('/', {
      templateUrl: 'templates/home.html'
    }).when('/dashboard', {
      templateUrl: '/templates/dashboard.html'
    }).otherwise({
      redirectTo: '/'
    });

    $locationProvider.html5Mode(true);
  }
]);

So I run cordova build ios and it does it's magic to make an xcode project, which I then open and run. I see THIS WORKS NOW! , but I don't see the contents of my home.html file.

What am I doing wrong?

You need bootstrap angular:

app.js is fine!

but remove ng-app inside the tag <body> and start angular when the event onDeviceReady has been triggered :

index.js file:

var app = {
    initialize: function () {
        this.bindEvents();
    },
    bindEvents: function () {
        document.addEventListener("deviceready", this.onDeviceReady, false);
    },
    onDeviceReady: function () {
        angular.element(document).ready(function () {

            angular.bootstrap(document, ["app"]);

        });
    }
};

normally that is all you need to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM