Angular ng-view不使用express

时间:2015-09-29 17:14:22

标签: javascript angularjs node.js express angular-routing

我正在使用ng-view尝试使用目录partials / home打开页面主页,而ng-view不使用express。当访问http://localhost:3000/home时,将打开一个包含渲染的新页面。为角定义的所有路线都没有效果。我想知道如何使用带有快递的ng-view在 index.html 中呈现我的页面 partials / home.html

app.js

//módulos
var express = require('express');
var load = require('express-load');
var bodyParser = require('body-parser');
var app = express();
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var server = require('http').Server(app);
var io = require('socket.io')(server);


// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');


//uncomment after placing your favicon in /public
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

load('routes').then('controllers').into(app);

server.listen(3000);
console.log('Site no ar ...');

module.exports = app;

路线快车 - index.js

module.exports = function (app) {

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

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

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

};

路线Angular - 位置:public / app / app.js

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

app.config(function($routeProvider, $locationProvider){

// remove o # da url
$locationProvider.html5Mode(true);

$routeProvider

    .when('/', {
        templateUrl : 'index.html',
        controller  : 'mainController',
    })


    .when('/home', {
        templateUrl : 'partials/home.html',
        controller  : 'homeController',
    })


    .otherwise ({ redirectTo: '/' });
});

1 个答案:

答案 0 :(得分:0)

我建议将index.js文件更改为:

module.exports = function (app) {
app.use(express.static('/partials')); //here you can write your partials directory. 
app.get('*', function(req, res){
    res.render('index');
});

};

(同时使用partials前缀更改angular文件中的templateUrl。);

那种方式将会加载角度,而角度路由器将处理从静态partials文件夹中获取的所有视图