【问题标题】:Tried to load angular more than once asp .net mvc试图不止一次加载角度asp .net mvc
【发布时间】:2015-10-08 08:00:30
【问题描述】:

我在运行我的 asp .net mvc 项目时收到类似“尝试多次加载 Angular”的警告,可以帮助我解决这个问题

我的 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>

【问题讨论】:

    标签: angularjs asp.net-mvc


    【解决方案1】:

    尝试改变路线顺序:

    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 时返回索引文件而不是部分的原因。您在索引文件中引导角度,所以您引导它两次。

    【讨论】:

    • 我改变了顺序,但我仍然得到同样的错误
    • 查看chrome的网络标签,调用Dashboard1/demoOne时的响应是什么
    猜你喜欢
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 2016-07-28
    • 2014-06-23
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多