【问题标题】:Angular JS with ASP.NET Master Pages带有 ASP.NET 母版页的 Angular JS
【发布时间】:2015-08-16 05:04:23
【问题描述】:

我正在尝试通过 ASP.NET 应用程序使用 Angular JS。

我为我的主页(母版页)定义了应用程序,它运行良好。现在我在使用其他页面时遇到了多个问题。

虽然我定义了一个使用此母版页的子页面,但它必须使用自己的 App。所以

  1. 我们可以在哪里定义子页面上的应用和控制器属性。
  2. 如果我在 DIV 容器上定义它们,它不喜欢它。它说 - Argument 'cityPageCtrl' is not a function, got undefined

在我的母版页中,我添加了如下参考:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-cookies.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

    <script src="<%=ApplicationPath %>lib/js/ui-bootstrap-tpls-0.12.1.min.js"></script>
    <script src="<%=ApplicationPath %>cust/js/templateApp.js"></script>
    <script src="<%=ApplicationPath %>cust/js/mainPageApp.js"></script>
    <script src="<%=ApplicationPath %>lib/js/parallax.min.js"></script>

在我的子页面中,下面是我必须使用另一个 ng 应用程序的代码:

<asp:Content ID="Content1" ContentPlaceHolderID="headContent" runat="server">
    <script src="../cust/js/cityPageApp.js"></script>
    <title ng-bind="'Restaurants in '+cityName+' | Restaurants'">Restaurants in your city serving online | Online Restaurnats</title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="bodyContent" runat="server">
   <div ng-app="cityPage" ng-controller="cityPageCtrl">
</div>
</asp:Content>

以下是我在 cityPage js 文件中为 Angular 定义应用程序的内容。

var app = angular.module('cityPage', ['ui.bootstrap', 'templateModule', 'ngCookies']);
app.controller("cityPageCtrl", function($scope, $http, $modal, $cookies, $interval)

【问题讨论】:

    标签: javascript jquery asp.net angularjs


    【解决方案1】:

    理想情况下,您应该为此创建多个模块。您的子页面应创建自己的模块,然后将其添加为对主模块的引用。

        var mainApp= angular.module('main', ['childApp']);  //child 1
    

    您也可以尝试使用 angular.bootstrap 手动引导

        angular.bootstrap(document.getElementById("divId"), ['myChildModule']);
    

    【讨论】:

      【解决方案2】:

      您可以使用named sections添加每个子页面的脚本文件。

      在您的母版页中:

          <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
          <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-cookies.min.js"></script>
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
      
          <script src="<%=ApplicationPath %>lib/js/ui-bootstrap-tpls-0.12.1.min.js"></script>
          <script src="<%=ApplicationPath %>cust/js/templateApp.js"></script>
          <script src="<%=ApplicationPath %>cust/js/mainPageApp.js"></script>
          <script src="<%=ApplicationPath %>lib/js/parallax.min.js"></script>
      
          @RenderSection("AngularScripts", required: false)
      

      在您的子页面中:

      @section AngularScripts
      {
         <script src="../cust/js/cityPageApp.js"></script>
      }
      

      您应该只引用母版页中的一般脚本文件,并将其他文件移动到适当的子页。例如,如果“mainPageApp.js”仅用于一页,则将其从母版页中删除并放入子页中。

      【讨论】:

      • 从外观上看,命名部分适用于 Razor 而不是 ASPX 语法。我的应用程序是一个 ASP.NET 而不是 MVC。其次,mainApp 让我的数据显示在顶部的导航栏和底部的页脚中。因此,它在母版页中是必需的。
      • 哦!抱歉,我不关注您的项目类型。我在 Web 表单应用程序中对其进行了测试。似乎问题不在于您的脚本标签在子页面中的位置。我猜您的母版页中有一个 ng-app。如果有,问题就在于,因为您的 Angular 应用程序中只能有一个 ng-app。
      • 那是正确的,这就是我需要解决的问题。如何在我的母版页中使用该应用程序来获取导航栏、页脚和其他常见位置的详细信息,同时仍继续将本机应用程序用于内容页面。目前我已经诉诸不使用母版页,这似乎很好。但是必须有一个我不知道的出路:(
      • 你的页面中不能有多个 ng-app,但是你可以有多个 ng-controllers 和嵌套的。您可以将子页面的控制器添加到主应用程序模块中。如果你的“mainPageApp”中有这样的东西:var app = angular.module('mainPage', ['ui.bootstrap', 'templateModule', 'ngCookies']); 你可以在你的“cityPage”中有这个:app.controller("cityPageCtrl", function($scope, $http, $modal, $cookies, $interval)。或者 angular.module(' mainPage').controller(... 这会将“cityPageCtrl”添加到“mainPage”模块,该模块在母版页的ng-app中设置。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多