简体   繁体   English

Angular ngRoute导致无限循环和堆栈溢出

[英]Angular ngRoute causes infinity loop and stack overflow

I might be a bit thick here, but I was trying to get ng-view and ngRoute working, and thought I got it all working, but it seems like the whole thing is just looping. 我在这里可能有点厚,但是我试图让ng-view和ngRoute工作,并以为我一切都正常了,但似乎整个过程都在循环。

To explain, it's a node server, using express, jade, angular. 解释一下,它是一个节点服务器,使用express,jade,angular。 It is set up so that the call should only go to layout, which then opens the routes in ngroute and renders based on that. 设置它的目的是使调用仅转到布局,然后布局在ngroute中打开并基于此进行渲染。 So this is my layout.jade 这是我的layout.jade

doctype html
html(ng-app="goMinute")
  head
    title= title
    base(href="/")
    link(rel='stylesheet', href='/stylesheets/style.css')
  banner
    .banner GoMinut
        p.banner-hit The place for simple meeting minutes

  body(ng-controller='BodyController as app')
      div.content
          ng-view


    script(src='/bower_components/angular/angular.js')
    script(src='/bower_components/angular-route/angular-route.js')
    script(src='/bower_components/angular-route/angular-route.js')
    script(src='/bower_components/angular-resource/angular-resource.js')
    script(src='/bower_components/jquery/dist/jquery.js')
    script(src='/bower_components/bootstrap/dist/js/bootstrap.min.js')
    script(src='/javascripts/goMinute.js')

So far so god, had to set base to something, and / seems to make it happy (not certain if this call this infinity loop This then calls goMinute.js which contains the routes, and it SEEMS like they are called 到目前为止,到目前为止,必须为某些事情设置基础,并且/似乎会让它高兴(不确定是否调用此无限循环。然后调用包含路线的goMinute.js,并像调用它们一样进行SEEMS

(function() {
    var goMinute = angular.module('goMinute', ["ngResource", "ngRoute"]).
        config(['$routeProvider', '$locationProvider',
            function ($routeProvider, $locationProvider) {
                $locationProvider.html5Mode(true);
                $routeProvider
                    .when('/index', {
                        templateUrl: 'partials/index',
                        controller: 'IndexController'
                    })
                    .when('/login', {
                        templateUrl: '/partials/login',
                        controller: 'loginController'
                    })

                    .otherwise({redirectTo: "/login"});
            }
        ]
    );
    goMinute.controller('BodyController', function ($scope, $http) {
        console.log("I am body")
    })

    goMinute.controller('IndexController', function ($scope, $http) {
        console.log("This will be Index")
    })

    goMinute.controller('loginController', function ($scope, $http) {
        console.log("This will be login")
    });
})();

However, whenever I call the webpage (and idenpendent if I call /index, /login or other. It will go past "I am body" once, thenhit the IndexController about 500 times before throwing a stack overflow. Any thoughts? Oh, and at the time, it generates the layout, but does not seem to output the information in index partial. 但是,每当我调用该网页时(如果我调用/ index,/ login或其他方法,则该身份都将消失。它会经过“我是身体”一次,然后在抛出堆栈溢出之前击中IndexController约500次。有什么想法吗?当时,它生成布局,但似乎不输出索引局部的信息。

After much muching about I think I found the issue (and I was looking in the wrong place). 经过大量讨论之后,我认为我找到了问题所在(并且我在错误的位置查看)。

The problem was not in the router, or at least I don't think so. 问题不出在路由器上,或者至少我不这么认为。 The problem was with the express router. 问题出在快速路由器上。 I had originally set it up as follows: 我最初将其设置如下:

app.use('/', routes);
app.use('*', routes);
app.use('/users', users);
app.use('/partials', partials);

Which worked on another site. 哪个在另一个站点上工作。 But I started thinking, what if the loop is caused server side, so removed the * line 但是我开始思考,如果循环是由服务器端引起的,那么删除*行

app.use('/', routes);
app.use('/users', users);
app.use('/partials', partials);

Which seems to have done the trick. 似乎已经做到了。 Problem is that I added the * line because some example told me to, so there is of course the chance that I broke it now, and it just seems like it is working. 问题是我添加*行是因为某个示例告诉我这样做,所以我现在当然有机会破坏它,而且看起来好像正在工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM