【问题标题】:ngview not displaying my templatengview 不显示我的模板
【发布时间】:2014-04-22 01:54:16
【问题描述】:

我正在尝试使用 ngView 显示部分 html 内容,并在 angularjs 中路由,但从下面的代码中我不知道为什么我的模板没有显示在主视图中。

我想导航到 localhost\manageStaff。默认情况下 localhost 路由指向 Home.cshtml。

代码如下:

 **Home.cshtml**
       @{
      Layout = null;
    }

<!DOCTYPE html>

<html ng-app="myApp">
<head>
<meta name="viewport" content="width=device-width" />
<title>Home</title>
<link href="~/Content/Site.css" rel="stylesheet" />
<link href="~/Content/AdminHeader.css" rel="stylesheet" />
</head>
<body>
<div id="headerArea">
</div>
<div id="sideBar">
    <div id="ActiveMenu">

    </div>
</div>
<div id="mainContent" ng-view>

</div>
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/angular-route.min.js"></script>
<script src="~/Scripts/angular-resource.min.js"></script>
<script src="~/js/app.js"></script>

 </body>
 </html>

**app.js**
    angular.module('myApp', ['ngResource','ngRoute'])
      .config(function ($routeProvider, $locationProvider) {
     $routeProvider.when('/manageStaff', {
       templateUrl: 'js/Templates/ManageStaffTemplate.html'
      //controller:'manageStaffCtrl'
      });

     $locationProvider.html5Mode(true);
  })

路由配置

 public class RouteConfig
 {
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

**Main Content Template**
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
Manage Staff
</body>

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    您的模板不应是完整的 HTML 文档,请将您的 Main Content Template 更改为:

    <div>Manage Staff</div>
    

    嵌套的 html/head/body 标签无效。

    【讨论】:

    • 谢谢。但仍然收到 404 错误。解决方案文件夹层次结构到模板。 root/js/Templates/ManageStaffTemplate.html
    • 没有看到任何关于:404,你可以直接在浏览器中点击该路线吗?如果没有,请确保您没有通过 web.config 或 MVC 路由中的任何设置阻止它
    猜你喜欢
    • 2014-02-25
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    相关资源
    最近更新 更多