角度视图无法正常运行

时间:2014-01-08 22:26:20

标签: javascript angularjs url-routing

我不知道为什么,但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 个答案:

答案 0 :(得分:2)

路由提供程序是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模块。