user2615699
user2615699

Reputation:

loading jade template through angular

I am working on a single page web app, and I wanted to load in jade views with my angular template but I am struggling to wrap my head exactly how to do so.

Here is my angular template: index.html

    <!doctype html>
<html ng-app="test">
  <head>
    <title>My Angular App</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
    <script type="text/javascript" src="/javascripts/app2.js"></script>

  </head>
  <body>
    <p> Hello </p>
    <div>
        <div ng-view></div>
    </div>
  </body>
</html>

I made a controller for my angular template: navigation.js

angular.module('test', [])
    .config(viewRouter);

function viewRouter ($routeProvider){
    $routeProvider
        .when('/', {templateURL: 'views/index.jade'}); 

}

Here I am trying to use a jade template to render onto the page, but it does not seem to be working.

I have a view jade templates,

index.jade

extends layout

block content
    h1= title

    include partials/menu

    .views-wrapper
       include partials/login 

layout.jade:

doctype
html
    head
        title= title
        link(rel='stylesheet', href='/stylesheets/style.css')

        body
            block content


            script(src='//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js')
            script(src='/javascripts/navigation.js')

menu.jade:

ul
    li.a VIEW A
    li.b VIEW B
    li.c VIEW C
    li.l VIEW LOGIN2

I also have a view simple templates (named a.jade, b.jade, and c.jade) which just have simple headers displaying a name as a test to see if the routing works. I am struggling to get these templates to connect, I can't seem to wrap my head around, nor find an answer as to how I can display my jade views through my angular template. Before I was not using an angular template, but decided to use one in order to deal with URL more easily.

this is my app.js on my server side:

/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.cookieParser('cookies monster')); // Cookie secret

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.set('public', path.join(__dirname, 'public'));
/*
 * Views
 */

//app.get('/', routes.index);
app.get('/', function(req, res, next){
    return res.sendfile(app.get('public') + '/index.html');
});
app.get('/users', user.list);
app.get('/a', routes.a);
app.get('/b', routes.b);
app.get('/c', routes.c);
app.get('/login2', routes.login2); 


http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

and this is a routes controller that I had to render the views before I decided to use angular

index.js:

exports.index = function(req, res){
  res.render('index', { title: 'New Test' });
};

// View A
exports.a = function(req, res) {
    res.render('partials/a', { layout: false, test: 'a' });
};

// View B
exports.b = function(req, res) {
    res.render('partials/b', { layout: false, test: 'b' });
};

exports.c = function(req, res) {
    res.render('partials/c', { layout: false, test: 'c' });
};

exports.login2 = function(req, res) {
    res.render('partials/login2', { layout: false, test: 'login2' });
};

I know this is a lot of code to look at, but I would really appreciate any help.

Upvotes: 4

Views: 13109

Answers (1)

Alex Choroshin
Alex Choroshin

Reputation: 6187

Check my answer on stackoverflow about using AngularJS seed for working with Jade, it can help you with your question.

Upvotes: 3

Related Questions