angular-route jquery依赖

时间:2014-09-17 17:11:23

标签: jquery angularjs angular-routing angularjs-ng-route

index.html(布局页面):

<!DOCTYPE html>
<html data-ng-app="web">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <script src="js/jquery.min.js"></script> <!-- v2.1.1 -->
    <script src="js/angular.min.js"></script> <!-- v1.2.24 -->
    <script src="js/angular-route.min.js"></script> <!-- v1.2.24 -->
    <script type="text/javascript">
    var web = angular.module("web", ['ngRoute']);
    web.config(function($routeProvider) {
        $routeProvider.when('/', {
            templateUrl: 'page1.html'
        }).otherwise({
            redirectTo: '/'
        });
    });
    </script>
</head>
<body data-ng-view>
</body>
</html>

page1.html(子页面):

<script type="text/javascript">
function myController($scope) {
    $scope.foo='foo';
}
</script>

<div data-ng-controller="myController">
    {{foo}}
</div>

如果从主页面(index.html)中删除jQuery,则angular-route无法找到控制器(myController)。任何人都可以告诉我这是一个错误还是我做错了什么?

我正在使用angular.min.js v1.2.24,angular-route.min.js v1.2.24和jquery.min.js v2.1.1。

1 个答案:

答案 0 :(得分:0)

你能试试这个吗? (未经测试)

的index.html

<!DOCTYPE html>
<html data-ng-app="web">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <script src="js/angular.min.js"></script> <!-- v1.2.24 -->
    <script src="js/angular-route.min.js"></script> <!-- v1.2.24 -->
    <script type="text/javascript">
    var web = angular.module("web", ['ngRoute']);
    web.controller('myController', function($scope) {
        $scope.foo='foo';
    });
    web.config(function($routeProvider) {
        $routeProvider.when('/', {
            templateUrl: 'page1.html',
            controller: 'myController'
        }).otherwise({
            redirectTo: '/'
        });
    });
    </script>
</head>
<body data-ng-view>
</body>
</html>

page1.html

<div>
    {{foo}}
</div>

参考:https://docs.angularjs.org/api/ngRoute/service/ $ route