Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-view not working with partials in AngularJS/Express

Everything was working until I tried to display a partial file in ng-view.

/public/app/app.js

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";
});

/server/includes/layout.jade

doctype html 5
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

/server/includes/scripts.jade (version number are not in the script)

script(type="text/javascript", src="/vendor/jquery/jquery.js") 2.1.0
script(type="text/javascript", src="/vendor/angular/angular.js") 1.2.16
script(type="text/javascript", src="/vendor/angular-resource/angular-resource.js")
script(type="text/javascript", src="/vendor/angular-route/angular-route.js")
script(type="text/javascript", src="/app/app.js")

/views/index.jade

extends ../includes/layout

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

/views/partials/main.jade

h1 This a Partial
h2 {{ myVar }}

and finally server.js

var express = require('express'),
stylus = require('stylus');

var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';

var app = express();

function compile(str, path){
return stylus(str).set('filename', path)
}

app.configure(function (){
  app.set('views', __dirname + '/server/views');
  app.set('view engine', 'jade');
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(stylus.middleware(
  {
      src: __dirname + '/public',
      compile: compile
  }
  ));
  app.use(express.static (__dirname + '/public'));
});

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

app.get('*', function(req, res) {
 res.render('index'); 
});

var port = 3030;
app.listen(port);
console.log('Listening on port' + port + '…');

Directory structure: structure of the site

When I launch the 'node server.js' and go to 'localhost:3030', I get a blank page instead of "This is a Partial Hello Angular" with this HTML:

<!DOCTYPE html 5>
<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="ng-view"></div>
</section>
<script type="text/javascript" src="/vendor/jquery/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></html>

In the tutorial this config works perfectly, but when I run it, the ng-view is empty. Any idea why?


EDIT:

Node.js runs like a charm. Something must have changed in the conf or I forgot something. In the tutorial everything was installed with bower like I did, and the javascript, hrefs were pointing to /vendor/ in the public directory and it seemed like it worked.

When I looked in the Javascript console, I got this error message for all scripts: SyntaxError: Unexpected token '<' which < is the beginning of an HTTP error message.

So I copied all the called files with error into /vendor/ change the url in "/server/includes/scripts.jade" like "/vendor/jquery/jquery.js" to "vendor/jquery.js" and it worked.

But I feel like I applied a patch. With bower, shouldn't it work with the previous settings?

like image 711
Lefty Avatar asked Nov 10 '22 08:11

Lefty


1 Answers

Not sure how much this will help but I notice all the bower packages are in the 'bower_components' directory. That is the default for bower unless you specify another directory. So, if you want the bower components installed say, '/public/vendor', do the following:

1) create a file called ' .bowerrc ' and save it in your root directory ie. with gitignore, bower.json, etc.

2) in the file put:

{
  "directory": "public/vendor"
}

That will load the bower components where you want them. The file ' .bowerrc ' is a config file for bower and you can find the guidance at: http://bower.io/

HTH

like image 129
kim bellinger Avatar answered Nov 15 '22 11:11

kim bellinger