简体   繁体   English

具有MVC的Spa模型中的AngularJS路由无限循环

[英]AngularJS Routing infinite loop in spa model with mvc

I have tried a bunch of other posts over the web that seems to be the same problem as mine, but none worked. 我在网上尝试了许多其他帖子,这些帖子似乎与我的问题相同,但均无济于事。

I have this menu: 我有这个菜单:

<ul class="nav navbar-nav">
    <li class="active"><a href="#Painel/1">Novas</a></li>
    <li><a href="#Painel/2">Todas</a></li>
    <li><a href="#Painel/3">Finalizadas</a></li>
    <li><a href="#Painel/4">Pendentes</a></li>
</ul>

Quite simple nav bar. 相当简单的导航栏。 This routing setup: 此路由设置:

module.config(["$routeProvider", "$locationProvider", function($routeProvider, $locationProvider)
{
    //$locationProvider.html5Mode(true);

    $routeProvider
        .when('/Painel/:painelId/', 
        {
            templateUrl : function(params)
            {
                return "/Painel/Index/" + params.painelId;
            },
            controller  : 'painelController'
        })
        .otherwise(
        {
            redirectTo: "/Painel"
        });
    }]);
}

My MVC Controller: 我的MVC控制器:

public class PainelController : Controller
{
    public ActionResult Index(int? id))
    {
        return View();
    }
}

And finally my angular controller: 最后是我的角度控制器:

module.controller('painelController', ["$scope", "$routeParams", function ($scope, $routeParams)
{
    debugger;
}]);

Its a very simple architecture, but angular is looping through its controller infinitely. 它是一个非常简单的体系结构,但是angular无限循环通过其控制器。 I took care to set my templateUrl relative with / as many posts suggested over there, but it didn't worked. 我小心地将我的templateUrl设置为带有/相对位置,因为那儿有很多建议,但这没有用。 It works sending the parameter to the mvc Action, but then it starts the loop stopping forever on the angular's controller debugger . 它可以将参数发送到mvc Action,但随后会开始在Angle的控制器debugger上永久停止循环。

What can I do? 我能做什么?

Your problem is that :painelId is not optional and thus you fall into your otherwise() redirect infinitely. 您的问题是:painelId不是可选的,因此您陷入无限的else()重定向中。 Make the :painelId optional like 将:painelId设为可选,例如

'/Painel/:painelId?/'

And this should fix your issue. 这应该可以解决您的问题。 If you are not using a version of Angular that supports optional parameters then you will need to add another when() to cover the '/Painel' route. 如果您未使用支持可选参数的Angular版本,则需要添加另一个when()来覆盖“ / Painel”路由。

The main problem wasn't on the Angular code, but in the MVC's return Action: 主要问题不在Angular代码上,而是在MVC的return Action中:

return View();

Since you're working with dynamic content, you don't have to return the full html source(with html , head , body tags...), so I had to change to: 由于您正在使用动态内容,因此不必返回完整的html源(带有htmlheadbody标签...),因此我不得不更改为:

return PartialView();

What makes totally more sense. 什么更有意义。 Because every request was calling a new angular code, that opened a new request and so on... Then I have created two routes for the opitional parameters(I don't know why it doesn't worked as @Tacoman667 pointed) 因为每个请求都在调用一个新的角度代码,所以打开了一个新请求,依此类推...然后,我为可选参数创建了两条路由(我不知道为什么它不能像@ Tacoman667所指出的那样起作用)

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

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