我不知道为什么,但是JavaScript不起作用。
当我单击链接时,视图未加载
<html ng-app>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("app");
app.config(function ($routeProvider) {
$routeProvider.when('/', {
templateUrl: 'views/default.html',
controller: 'default'
});
$routeProvider.when('/menu', {
templateUrl: 'views/menu.html',
controller: 'menu'
});
$routeProvider.otherwise({ redirectTo: '/' });
});
app.service( 'api', function($http)
{
this.get_id=function (js_data,success_fn) {
$http({
url: '/get_id',
method: "POST",
data: js_data,
headers: {'Content-Type': 'application/json'}
}).success(function (json) {var data=angular.fromJson(json);success_fn(data);});
};
});
app.controller("default", function ($scope, $location) {
$scope.test = "default";
});
app.controller("menu", function ($scope, api) {
$scope.username = "UserName";
$scope.get_id=function() {
api.get_id(
$scope.username,
function (data)
{alert(data);}
);
}
});
</script>
</head>
<body>
<div >
<a href="#/">home</a><br />
<a href="#/menu">menu</a><br />
</div>
<div ng-view>
</div>
</body>
</html>
最佳答案
路由提供者是1.2分支中的外部依赖项。您需要添加:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-route.min.js"></script>
您还需要添加
ngRoute
依赖项:即。
angular.module('ngViewExample', ['ngRoute'])
http://docs.angularjs.org/api/ngRoute。$ routeProvider
依存关系
需要安装ngRoute模块。