【问题标题】:Angular and Asp.Net Mvc errorAngular 和 Asp.Net Mvc 错误
【发布时间】:2014-04-11 17:23:05
【问题描述】:

在名为 _LoginPartial.cshtml 的 Aps.net Mvc 视图中,我添加了 Angular 控制器:

    <div class="navbar-right" data-ng-app="PublicApp" data-ng-controller="PublicNotificationsCtrl">
  </div>

并且该登录部分使用以下方式呈现在我的默认布局上:

@Html.Partial("_LoginPartial")

在它下面我有

 @RenderBody()

哪个渲染主体有它的 Angular 控制器。

所以在 html 中这一切看起来像:

            <div class="navbar-right ng-scope" data-ng-controller="PublicNotificationsCtrl" data-ng-app="PublicApp">
                <ul class="nav navbar-nav">
                    <li class="dropdown user-profile-dropdown notifications-dropdown"></li>
                </ul>
                <ul class="nav navbar-nav">
                    <li class="dropdown user-profile-dropdown"></li>
                </ul>
            </div>
        </div>
        <!--

        /.nav-collapse 

        -->
    </div>
<div class="">
    <div  data-ng-controller="PublicRoutinesCtrl" data-ng-app="PublicApp">
        <div class="container" data-ng-show="isMainScreen"></div>
        <div class="container special-container" data-ng-hide="isMainScreen"></div>
    </div>
</div>

但是我的其他控制器不再工作,就好像没有加载角度一样(例如,我看到 {{test.Value}} 而不是实际值)

在控制台中我没有收到任何错误。为什么会这样?

编辑

我在我的主布局页面中放置了空白 div,如下所示:

<div data-ng-app="PublicApp" data-ng-controller="PublicNotificationsCtrl">

                </div>

但结果相同

【问题讨论】:

  • 您的其他控制器是否仍在您的根目录ng-app div 中?
  • 我每次调用控制器时都没有 root ng-app 我这样做:&lt;div data-ng-app="PublicApp" data-ng-controller="PublicNotificationsCtrl"&gt;

标签: javascript asp.net-mvc angularjs


【解决方案1】:

根据您使用每个 div 实例化 ng-app 的评论,这听起来是个问题。每个页面只能有一个ng-app,通常是页面中的bodyhtml 元素。

来自角度文档:

每个 HTML 文档只能自动引导一个 AngularJS 应用程序。在文档中找到的第一个 ngApp 将用于定义根元素以自动引导为应用程序。要在 HTML 文档中运行多个应用程序,您必须使用 angular.bootstrap 手动引导它们。 AngularJS 应用程序不能相互嵌套。 -- http://docs.angularjs.org/api/ng.directive:ngApp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 2013-03-03
    相关资源
    最近更新 更多