AngularJS ng-view未显示视图

时间:2021-11-10 13:12:05

I'm developing a web app with AngularJS for the frontend and i'm trying to set the routing and showing the views, so far even ng-include and other stuff work for me but not ng-view I'm developing in visual studio and when i run the app it's using a local server (localhost)

我正在开发一个带有AngularJS的Web应用程序用于前端,我正在尝试设置路由并显示视图,到目前为止,甚至ng-include和其他东西对我有用但不是ng-view我正在开发Visual Studio当我运行应用程序时,它使用本地服务器(localhost)

here's my directory structure

这是我的目录结构

- ASP.NET project
---- Css
---- Scripts
-------- App
-------- app.js
----------- Home
-------------- Controllers
------------------ index-controller.js
---- Views
------- Partials
---------- navbar.html
------- home.html
- index.html

Here's mi 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>

Here's my app.js

这是我的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 */

When i load the site it loads correctly and if i see the requests i can clearly see that it's requesting the home.html with 304 status code, the thing is the div with ng-view is not showing the html inside home.html, what could this be due to?

当我加载网站时,它正确加载,如果我看到请求,我可以清楚地看到它正在请求home.html与304状态代码,事情是div与ng-view没有显示home.html内的html,什么这可能是由于?

2 个解决方案

#1


0  

I've solved the issue by changing the <ng-include> tag with an <div ng-include="srctoview"> tag. If anyone is having this issue i'd recommend doing this, or doing it like in this answer using a controller

我已经通过使用

标记更改 标记来解决了这个问题。如果有人遇到这个问题,我建议你这样做,或者像在这个答案中那样使用控制器

Thanks to @Sourabh- for leading me to an answer.

感谢@ Sourabh-带领我回答。

#2


0  

close <ng-include> directive properly as browser is wrapping ur <ng-view> inside of <ng-include>. it will solve the problem. as @pankaj has already suggested.

当浏览器在 中包装你的 时,正确关闭 指令。它会解决问题。正如@pankaj已经建议的那样。

#1


0  

I've solved the issue by changing the <ng-include> tag with an <div ng-include="srctoview"> tag. If anyone is having this issue i'd recommend doing this, or doing it like in this answer using a controller

我已经通过使用

标记更改 标记来解决了这个问题。如果有人遇到这个问题,我建议你这样做,或者像在这个答案中那样使用控制器

Thanks to @Sourabh- for leading me to an answer.

感谢@ Sourabh-带领我回答。

#2


0  

close <ng-include> directive properly as browser is wrapping ur <ng-view> inside of <ng-include>. it will solve the problem. as @pankaj has already suggested.

当浏览器在 中包装你的 时,正确关闭 指令。它会解决问题。正如@pankaj已经建议的那样。