回答幾個問題和論壇搜索後,我終於得到它工作可靠。這是我從一個乾淨的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