I want to set up angular with ng-route using JADE. I cannot connect to my templates(where I am storing my html files). Below are the steps. I apologize that it is long, but I wanted to show everything involved Any advice to setting up angular with express is much appreciated!!!!
var app = express();
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({extended: false}));
In my layout.jade I identified all of the script tags that I will be using.
script(src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.js")
script(src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular-resource.js")
script(src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular-route.js")
script(src="/client.js")
In my index.jade
extends layout
block content
div(ng-view)
I created a folder inside of my views and am housing my home page there. 文件夹,并在其中放置我的主页。
Here is are the contents of my . 的内容。
var beerApp = angular.module('beerApp',['ngResource','ngRoute']);
beerApp.config(function($routeProvider){
$routeProvider
.when('/',{
templateUrl:'/templates/home',
controller: 'homeController'
})
});
beerApp.controller('homeController',function(){
console.log('controller set up')
});
My first assumption was that inside of my controllers I must need to set up request for the so I did. 设置请求,所以我这样做了。
var indexController = {
index: function(req, res) {
res.render('index');
},
getTemplate: function(req,res){
res.render('templates/', + req.params.templateid);
}
};
module.exports = indexController;
Rendering templates did nothing to fix my error. Home.html is not being found in my chrome dev tools.
routes/templates.js
var express = require('express');
var router = express.Router();
/* GET template by name */
router.get('/:templateid', function(req, res, next) {
res.render('templates/' + req.params.templateid);
});
module.exports = router;
app.js - so you have access to templates by url, eg. templates/home
- check if broweser can render this template
app.use('/templates', require('./routes/templates'));
templates/home.jade - use pipe for marking text as raw content
p
| {{ hello }}
client.js
beerApp.controller('homeController', function ($scope) {
$scope.hello = 'hello world';
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.