【问题标题】:Multiple Views in AngularJsAngularJs 中的多个视图
【发布时间】:2015-10-21 09:50:37
【问题描述】:

我有一个索引页和三个部分视图。索引页面使用 ng-view 动态加载部分视图。部分视图称为 TestPartial1.html、TestPartial2.html 和 BothPartials.html。 BothPartials.html 包含 TestPartial1.html 和 TestPartial2.html,因此当点击 BothPartials 链接时,ng-view 会调用 BothPartials.html,它应该同时加载 TestPartial1.html 和 TestPartial2.html。

我可以分别调用和显示 TestPartial1.html 和 TestPartial2.html,但我不知道如何通过 BothPartials.html 一起显示两个页面。我曾尝试在 BothPartials.html 中使用 ng-include 来加载 TestPartial1 和 TestPartial2 但无济于事。

我的代码如下:

Index.aspx

<body ng-app="myApp">
 <div ng-view id="ng-view"></div> 
</body>

TestPartial1.html

<div>
 <h1>View 1</h1><br />
 <a href="#View2">View2</a>
</div>

TestPartial2.html

<div>
 <h1>View 2</h1><br />
 <a href="#View1">View1</a>
 <a href="#BothViews">Both Views</a>
</div>

BothPartials.html

<div>
 <br />
 <br />
 test
 <div ng-include = "TestPartial1.html"></div>
 <div ng-include = "TestPartial2.html"></div>
</div>

App.js

(function () {
var myApp = angular.module('myApp', ['ngRoute']);    
myApp.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
        .when('/Index.aspx', {
            templateUrl: 'PartialViews/TestPartial1.html'                
        })
        .when('/View1', {                
            templateUrl: 'PartialViews/TestPartial1.html'              
        })
        .when('/View2', {               
            templateUrl: 'PartialViews/TestPartial2.html'

        })
        .when('/BothViews', {
            templateUrl: 'PartialViews/BothPartials.html'

        })
        .otherwise({ redirectTo: '/Index.aspx' });

}]);
}
)();

【问题讨论】:

  • 可能是因为您的ng-include 是相对路径。试试绝对路径:ng-include="/PartialViews/TestPartial1.html"
  • 如果你想要多个视图,长期使用 ui-router 可能会更好
  • @Coop 我尝试了以下不同的组合,但没有用:“~/Restricted/PartialViews/TestPartial1.html”、“/PartialViews/TestPartial1.html”、“/TestPartial1.html”跨度>
  • 控制台中是否可见错误?
  • @Coop,控制台没有错误。我接受了莫里西的建议并使用了 ui-router。感谢您的帮助。

标签: angularjs ngroute


【解决方案1】:

在 ng-include 中为 TestPartial1.html 和 TestPartial2.html 提供正确的路径。

【讨论】:

  • 我尝试了以下不同的组合但没有用:“~/Restricted/PartialViews/TestPartial1.html”、“/PartialViews/TestPartial1.html”、“/TestPartial1.html”
猜你喜欢
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 2015-11-05
  • 1970-01-01
  • 2015-03-23
  • 1970-01-01
  • 1970-01-01
  • 2017-09-25
相关资源
最近更新 更多