路由功能陷入无限循环

时间:2015-04-19 16:04:01

标签: javascript angularjs

我正在尝试学习AngularJS,这是我写的代码:

myApp.config(function($routeProvider){
    console.log('--Inside config--');
    $routeProvider
    .when('/',{
        templateUrl: 'index.html'
    })
    .when('/list', {
        templateUrl: 'list.html'
    })
    .when('/detail', {
        templateUrl: 'detail.html'
    })
    .otherwise({
        redirectTo: '/'
    });
});

当我在Chrome中查看时,JS控制台显示它已进入无限循环并冻结。一旦我删除此功能,它就会很好。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

在HTML文件中试试这个:

<html ng-app="My_App">
<head>
    <script data-require="angular.js@1.0.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js" data-semver="1.0.7"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>        
    <script src="<path to test.js>/test.js"></script>
</head>
<body ng-view>
</body>
</html>

test.js:

var app = angular.module('My_App', []);

app.config(['$routeProvider', function($routeProvider){

$routeProvider
.when('/main',{
    templateUrl: 'partials/main_navigation/main.html',
    controller: 'main' // If you want to use controllers
)
.when('/menu',{
    templateUrl: 'partials/main_navigation/menu.html',
    controller: 'menu' // If you want to use controllers
).otherwise({redirectTo: '/main'});