angular js ng-view返回blanc partials - Express / Jade

时间:2021-12-09 03:13:43

I am writing an angular application following a tutorial. To my surprise, i followed exactly and my node js is starting fine without an issue. However, my angular is not returning the template as suppose to . I saw a similar problem here same tutorial and the same issue. However, the question was not answered . Below is my dir structure

我正在按照教程编写一个角度应用程序。令我惊讶的是,我完全按照我的节点js开始没问题。但是,我的角度并没有按照假设返回模板。我在同一个教程和同样的问题中看到了类似的问题。但是,问题没有得到解答。下面是我的目录结构

angular js ng-view返回blanc partials  -  Express / Jade

app.js

var app = angular.module('app', ['ngResource', 'ngRoute']);

angular.module('app').config(function($routeProvider, $locationProvider){
    $locationProvider.html5Mode(true);
    $routeProvider
        .when('/', { templateUrl: 'partials/main', controller: 'mainCtrl'});
});


angular.module('app').controller('mainCtrl', function($scope){
    $scope.myVar = "Hello Angular";
});

My layout.jade

doctype html
head
   link(rel="styleSheet",href="css/bootstrap.css")
   link(rel="styleSheet",href="vendor/toastr/toastr.css")
   link(rel="styleSheet",href="css/site.css")
body(ng-app='app')
   block main-content
   include scripts

My main.jade

h1 This is a partial
h2 {{ myVar }}

The route in my server.js are set as

我的server.js中的路由设置为

app.get('partials/:partialPath',function(req,res){
    res.render('partials/' + req.params.partialPath);
});
app.get('*', function(req,res){
    res.render('index');
});

my index.jade

extends ../includes/layout

block main-content
    section.content
       div(ng-view)

Althought i am thinking that shouldn't be an issue because i am starting with a partial view which is part of a page. When i run, my page return black. I inspect the element and ensured that all the js and css where loaded. When i view the source, a html source below was generated.

虽然我认为这应该不是一个问题,因为我从部分视图开始,这是页面的一部分。当我跑,我的页面返回黑色。我检查元素并确保所有js和css在哪里加载。当我查看源代码时,会生成下面的html源代码。

<!DOCTYPE html>
<head><link rel="styleSheet" href="css/bootstrap.css">
<link rel="styleSheet" href="vendor/toastr/toastr.css">
<link rel="styleSheet" href="css/site.css">
</head>
<body ng-app="app">
  <section class="content">
   <div ng-view></div></section>
   <script type="text/javascript" src="vendor/jquery/dist/jquery.js"></script><script type="text/javascript" src="vendor/angular/angular.js"></script>
<script type="text/javascript" src="vendor/angular-resource/angular-resource.js"></script>
<script type="text/javascript" src="vendor/angular-route/angular-route.js"></script><script type="text/javascript" src="app/app.js"></script>
</body>

I was suspecting routeProvider from my app.js here

我怀疑我的app.js中的routeProvider

.when('/', { templateUrl: 'partials/main', controller: 'mainCtrl'});

tried

.when('/', { templateUrl: '/partials/main', controller: 'mainCtrl'});

All to no avail . please where do i go wrong ? I have tried everything possible. I even restarted the tut yet still blanc. any help would be appreciated.

一切都无济于事。请问哪里出错了?我尽力了。我甚至重新启动了啧啧但还是空白。任何帮助,将不胜感激。

1 个解决方案

#1


Thank you for your time. It turns out the issue is in my layout

感谢您的时间。事实证明问题出在我的布局中

doctype html
head
   link(rel="styleSheet",href="css/bootstrap.css")
   link(rel="styleSheet",href="vendor/toastr/toastr.css")
   link(rel="styleSheet",href="css/site.css")
body(ng-app='app')
   block main-content
   include scripts

Changed to

 doctype html
    head
       base(href='/')
       link(rel="styleSheet",href="css/bootstrap.css")
       link(rel="styleSheet",href="vendor/toastr/toastr.css")
       link(rel="styleSheet",href="css/site.css")
    body(ng-app='app')
       block main-content
       include scripts

What was missing is

缺少的是

base(href='/')

It does not load the templates if you remove that base. I learnt that from another tut video by tut plus "[Tuts Plus] Building a Web App From Scratch With AngularJS Video Tutorial" . The presenter specifically mentioned the important of the base(href='/') and warned never to forget . Alternatively , there is another good example from @Alex Choroshin answer here . you should download the doc he suggested . Its a perfect example. Thanks

如果删除该基础,则不会加载模板。我从另一个tut视频中了解到tut加上“[Tuts Plus]使用AngularJS视频教程从头开始构建Web应用程序”。主持人特别提到基地的重要性(href ='/')并警告永远不要忘记。另外,还有来自@Alex Choroshin的另一个很好的例子。你应该下载他建议的文件。这是一个完美的例子。谢谢

#1


Thank you for your time. It turns out the issue is in my layout

感谢您的时间。事实证明问题出在我的布局中

doctype html
head
   link(rel="styleSheet",href="css/bootstrap.css")
   link(rel="styleSheet",href="vendor/toastr/toastr.css")
   link(rel="styleSheet",href="css/site.css")
body(ng-app='app')
   block main-content
   include scripts

Changed to

 doctype html
    head
       base(href='/')
       link(rel="styleSheet",href="css/bootstrap.css")
       link(rel="styleSheet",href="vendor/toastr/toastr.css")
       link(rel="styleSheet",href="css/site.css")
    body(ng-app='app')
       block main-content
       include scripts

What was missing is

缺少的是

base(href='/')

It does not load the templates if you remove that base. I learnt that from another tut video by tut plus "[Tuts Plus] Building a Web App From Scratch With AngularJS Video Tutorial" . The presenter specifically mentioned the important of the base(href='/') and warned never to forget . Alternatively , there is another good example from @Alex Choroshin answer here . you should download the doc he suggested . Its a perfect example. Thanks

如果删除该基础,则不会加载模板。我从另一个tut视频中了解到tut加上“[Tuts Plus]使用AngularJS视频教程从头开始构建Web应用程序”。主持人特别提到基地的重要性(href ='/')并警告永远不要忘记。另外,还有来自@Alex Choroshin的另一个很好的例子。你应该下载他建议的文件。这是一个完美的例子。谢谢