我在 PhoneGap 中遇到了 ngView 的问题。
一切似乎都加载得很好,我什至可以使用 ng-controller 获得一个基本的 Controller 。但是当我尝试将路由与 ngView 一起使用时,什么也没有发生。
索引.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>
应用程序.js
angular.module('App', []).config(function ($routeProvider) {
$routeProvider.when('/test', {
controller: TestCtrl,
template: '<h1> {{ test }} </h1>'
});
});
function TestCtrl($scope) {
$scope.test = "Works!";
}
Eclipse 记录器显示
onMessage(onPageFinished, fle:///android_asset/www/index.html#/test)
每次我点击链接,并在没有 #
的情况下尝试它只是引发找不到路径的错误。从我在其他地方准备的内容来看,这应该可以工作。任何帮助将不胜感激。
最佳答案
在搜索了几个问题和论坛之后,我终于让它可靠地工作了。这就是我从一个干净的 PhoneGap 项目中运行它的原因。
索引.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'
时,我们手动引导 Angular。事件。路由器.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。我个人不需要这个,但似乎有几个人遇到了这个问题,所以我把它包括在内。
Controller .js
function TestCtrl($scope) {
$scope.status = "It works!";
}
function ViewCtrl($scope) {
$scope.status = "Also totally works!";
}
最后,只是一些基本的 Controller 。
我已经创建了一个 github 存储库,其中包含所有这些 here .
希望这对其他人有帮助。
关于cordova - Angular ng-view/routing 在PhoneGap 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15105910/