具有快速路由的Angular + Jade问题

时间:2016-03-23 22:41:33

标签: angularjs node.js express pug

我在使用Angular& amp时遇到了一个问题。玉。一切都很好,快递路由工作正常(获取和发布)。我注意到,当我将ng-app添加到jade模板中的html或body标签时,帖子不再起作用了。客户端似乎正在工作,模板正在加载,但是一旦我尝试点击提交按钮的表单,发布请求不会被发送,只有获取。我甚至尝试在app.js中控制登录日志,但它似乎没有到达那里。

不确定它是否甚至与提交按钮有关,只是没有在表单上提交请求,因为当我点击它时,没有任何事情发生,所以可能是一个绑定问题?但是,如果我删除ng-app,一切都会正常工作。

layout.jade

doctype html
html(ng-app='myApp')
  head
    meta(charset='utf-8')
    meta(http-equiv='X-UA-Compatible', content='IE=edge')
    meta(name='viewport', content='width=device-width, initial-scale=1.0')
    meta(name='csrf-token', content=_csrf)
    title #{title} - Hackathon Starter
    link(rel='shortcut icon', href='/favicon.png')
    link(rel='stylesheet', href='/css/main.css')
  body
    include partials/header

    .container
      include partials/flash
      block content

    script(src='https://ajax.googleapis.com/ajax/libs/angularjs/1.5.2/angular.min.js')
    script(src='https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-resource.min.js')
    script(src='https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular-route.min.js')
    script(src='/js/lib/angular-route.min.js')
    script(src='/js/lib/angular-animate.min.js')
    script(src='/js/lib/angular-aria.min.js')
    script(src='/js/lib/angular-material.min.js')
    script(src='/js/lib/moment.js')
    script(src='/js/lib/angular-moment.min.js')
    script(src='/js/lib/angular-file-upload.min.js')
    script(src='/js/lib/jquery-2.2.0.min.js')
    script(src='/js/lib/bootstrap.min.js')
    script(src='/js/main.js')
    script(src='/js/bindings.js')

signup.jade

block content
  .page-header
    h3 Sign up
  form.form-horizontal(id='signup-form', method='POST')
    input(type='hidden', name='_csrf', value=_csrf)
    .form-group
      label.col-sm-3.control-label(for='email') Email
      .col-sm-7
        input.form-control(type='email', name='email', id='email', placeholder='Email', autofocus)
    .form-group
      label.col-sm-3.control-label(for='password') Password
      .col-sm-7
        input.form-control(type='password', name='password', id='password', placeholder='Password')
    .form-group
      label.col-sm-3.control-label(for='confirmPassword') Confirm Password
      .col-sm-7
        input.form-control(type='password', name='confirmPassword', id='confirmPassword', placeholder='Confirm Password')
    .form-group
      .col-sm-offset-3.col-sm-7
        button.btn.btn-success(type='submit')
          i.fa.fa-user-plus
          | Signup

app.js(部分内容)

/**
 * Module dependencies.
 */
var express = require('express');
var compress = require('compression');
var session = require('express-session');
var bodyParser = require('body-parser');
var logger = require('morgan');
var errorHandler = require('errorhandler');
var lusca = require('lusca');
var busboy = require('connect-busboy');
var methodOverride = require('method-override');
var dotenv = require('dotenv');
var MongoStore = require('connect-mongo/es5')(session);
var flash = require('express-flash');
var path = require('path');
var fs = require('fs-extra');
var mongoose = require('mongoose');
var passport = require('passport');
var expressValidator = require('express-validator');
var sass = require('node-sass-middleware');
var multer = require('multer');
var upload = multer({ dest: path.join(__dirname, 'uploads') });

/**
 * Load environment variables from .env file, where API keys and passwords are configured.
 *
 * Default path: .env (You can remove the path argument entirely, after renaming `.env.example` to `.env`)
 */
dotenv.load({ path: '.env.example' });

/**
 * Controllers (route handlers).
 */
var homeController = require('./controllers/home');
var userController = require('./controllers/user');
var apiController = require('./controllers/api');
var contactController = require('./controllers/contact');
var picksController = require('./controllers/picks');


/**
 * API keys and Passport configuration.
 */
var passportConfig = require('./config/passport');

/**
 * Create Express server.
 */
var app = express();

/**
 * Connect to MongoDB.
 */
mongoose.connect(process.env.MONGODB || process.env.MONGOLAB_URI);
mongoose.connection.on('error', function() {
  console.log('MongoDB Connection Error. Please make sure that MongoDB is running.');
  process.exit(1);
});

/**
 * Express configuration.
 */
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(compress());
app.use(sass({
  src: path.join(__dirname, 'public'),
  dest: path.join(__dirname, 'public'),
  sourceMap: true
}));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator());
app.use(busboy());
app.use(methodOverride());
app.use(session({
  resave: true,
  saveUninitialized: true,
  secret: process.env.SESSION_SECRET,
  store: new MongoStore({
    url: process.env.MONGODB || process.env.MONGOLAB_URI,
    autoReconnect: true
  })
}));
app.use(passport.initialize());
app.use(passport.session());

/**
 * Primary app routes.
 */
app.get('/', homeController.index);
app.get('/login', userController.getLogin);
app.post('/login', userController.postLogin);
app.get('/signup', userController.getSignup);
app.post('/signup', userController.postSignup);

1 个答案:

答案 0 :(得分:0)

尝试ng-submit for angular。 angular提供表格提交的ng-submit选项。
修改 还可以从控制台发布您的网络统计信息。它会有所帮助