试图加载角度超过一次asp .net mvc

时间:2015-10-08 08:00:30

标签: angularjs asp.net-mvc

我正在收到类似“尝试加载角度不止一次”的警告,同时运行我的asp .net mvc项目可以帮我解决这个问题

我的app.js

    angular.module('myApp', ['ngRoute']).config(['$routeProvider', function ($routeProvider) {
      $routeProvider.when('/dashboard1', { templateUrl: 'Dashboard1/demoOne', controller: 
'demoCtrl' }).otherwise({ redirectTo: '/' });
  }]);

我的RouteConfig.cs

 routes.MapRoute(
            name: "Default",
            url: "{*catchAll}",
            defaults: new
            {
                controller = "Home", action = "index", catchAll = UrlParameter.Optional
            } );
        routes.MapRoute(
            name: "Dashboard",
            url: "Dashboard1/{action}",
            defaults: new { controller = "Dashboard1", action = "Empty" });

_layout.cshtml

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery")
</head>
<body ng-app="myApp" ng-controller="myCtrl">
                   <ul >                      
                         <li> <a href="#/dashboard1" > link1 </a></li>
                     </ul>

                @Html.Partial("_LoginPartial")
            </div>
    <div data-ng-view></div>

        @RenderBody()
   @Scripts.Render("~/bundles/angular")
    <link href="~/Content/menu.css" rel="stylesheet" />
   <script src="~/Scripts/myapp.js"></script>   
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试更改路线顺序:

routes.MapRoute(
        name: "Dashboard",
        url: "Dashboard1/{action}",
        defaults: new { controller = "Dashboard1", action = "Empty" });

routes.MapRoute(
        name: "Default",
        url: "{*catchAll}",
        defaults: new
        {
            controller = "Home", action = "index", catchAll = UrlParameter.Optional
        } );

我认为代码中的第一个路由处理所有请求,而第二个路由永远不会执行。这就是为什么在请求Dashboard1/demoOne索引文件时返回而不是部分索引。你在索引文件中引导角度,所以你要引导它两次。