回答几个问题和论坛搜索后,我终于得到它工作可靠。这是我从一个干净的PhoneGap项目中运行它所需要的。
的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>
注意<html ng-app="App">
标签。该应用程序不会加载没有该指令的值,所以请确保您包含一个。
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);
});
},
};
在这个文件中,我们手动自举角时的PhoneGap将触发'ondeviceready'
事件。
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'
});
});
这个文件有两个重要的东西在里面。首先,我们在<html np-app="App">
中创建与之前使用的名称相同的模块。其次,我们需要将路由器配置为将文件URI列入白名单 。我个人并不需要这个,但有几个人似乎遇到了这个问题,所以我把它包括进去了。
controllers.js
function TestCtrl($scope) {
$scope.status = "It works!";
}
function ViewCtrl($scope) {
$scope.status = "Also totally works!";
}
最后,只是一些基本的控制器。
我创建了一个github回购,所有这一切here。
希望这可以帮助别人。
您应该使用'NG-href',而不是正常的'href'所以角可以正确地重写这些URL。 – 2015-03-03 00:40:36