我正在为AngonJS开发一个用于前端的Web应用程序,我正在尝试设置路由并显示视图,到目前为止,即使ng-include和其他内容对我有用,但不是ng-view 我正在开发visual studio,当我运行应用程序时,它使用的是本地服务器(localhost)
这是我的目录结构
- ASP.NET project
---- Css
---- Scripts
-------- App
-------- app.js
----------- Home
-------------- Controllers
------------------ index-controller.js
---- Views
------- Partials
---------- navbar.html
------- home.html
- index.html
这是mi index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Scolaris</title>
<!--Import materialize.css-->
<link type="text/css" rel="stylesheet" href="Content/materialize/css/materialize.css" media="screen,projection" />
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
</head>
<body ng-app="app_module">
<ng-include src="'Views/Partials/navbar.html'"/>
<div ng-view></div>
<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="Content/materialize/js/materialize.js"></script>
<script type="text/javascript" src="Scripts/angular.js"></script>
<script type="text/javascript" src="Scripts/angular-route.js"></script>
<script type="text/javascript" src="Scripts/App/app.js"></script>
<script type="text/javascript" src="Scripts/App/Home/Controllers/index-controller.js"></script>
<script type="text/javascript" src="Scripts/initialization.js"></script>
</body>
</html>
这是我的app.js
'use strict';
var app_module = angular.module('app_module', ['ngRoute']);
app_module.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: '/Views/home.html',
controller: 'indexController'
})
.otherwise({
redirectTo: '/'
});
}]);
/* tried with ../../Views/home.html, ../Views/home.html, etc and nothing */
当我加载网站时,它正确加载,如果我看到请求,我可以清楚地看到它正在请求带有304状态代码的home.html,那就是带有ng-view的div没有显示内部的html。 HTML,这可能是什么原因?
答案 0 :(得分:0)
我已使用&lt; <ng-include>
标记更改div ng-include="srctoview">
标记,从而解决了该问题。如果有人遇到这个问题,我建议你这样做,或者像在这个答案中那样做using a controller
感谢@ Sourabh-带领我回答。
答案 1 :(得分:0)
<ng-include>
包裹在<ng-view>
内时,正确关闭<ng-include>
指令。它会解决问题。正如@pankaj已经建议的那样。