angularjs - Angular js Expressions not working in Node js project -
i have configure node js default project express , using html page instead of jade. want use angular js within html pages. angular js simple examples working fine in project :
<!doctype html> <html lang="en-us"> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> <body> <div ng-app=""> <p>name : <input type="text" ng-model="name"></p> <p ng-bind="name"></p> </div> </body> </html>
its working fine. if using {{name}} instead of ng-bind="name", not working in project. example:
<!doctype html> <html lang="en-us"> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> <body> <div ng-app=""> <p>name : <input type="text" ng-model="name"></p> <h1>hello {{name}}</h1> </div> </body> </html>
this not working in node project. can me find-out issue.
app.js file code following:
var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieparser = require('cookie-parser'); var bodyparser = require('body-parser'); // sql server new code var connection = require('tedious').connection; var request = require('tedious').request; var webconfig = { user: 'sa', password: '#####', server: '######', database: 'test_training', options: { encrypt: false // use if you're on windows azure } } var sql = require('mssql'); // sql server new code var index = require('./routes/index'); var users = require('./routes/users'); var app = express(); // use simple html page var cons = require('consolidate'); // view engine setup app.engine('html', cons.swig) app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'html'); // use simple html page 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'))); app.use(express.static(__dirname + '/public')); app.use('/', index); app.use('/users', users); // catch 404 , forward error handler app.use(function(req, res, next) { var err = new error('not found'); err.status = 404; next(err); }); // error handler app.use(function(err, req, res, next) { // set locals, providing error in development res.locals.message = err.message; res.locals.error = req.app.get('env') === 'development' ? err : {}; // render error page res.status(err.status || 500); res.render('error'); }); module.exports = app;
help appreciated.
the html engine not supported out of box.
(ref: https://github.com/expressjs/express/wiki#template-engines)
solution given @thanh tùng work, if want set html view engine should follow solution
app.set('views', path.join(__dirname, 'views')); // set ejs view engine app.set('view engine','html'); // set html engine app.engine('html', require('ejs').renderfile);
later need place index.html inside /views directory
(original answer: https://stackoverflow.com/a/44945104/3627827)
edit: updated answer suggested @danielziga
Comments
Post a Comment