Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

430
Vistas
NodeJS - AngularJS Send JSON object and render templat at same time?

I have some problems with my api. I use NodeJS with ExpressJS Routing and AngularJS.

What I want to do is to render a template (ejs) and at the same time send a json object.

In my routes folder I have the index.js file with this (a little part) :

router.get('/data', function(req, res, next){
    Planning.getPlanningM(function(resultat){
        res.json(resultat);
        res.render('index');
    });
});

About the variable resultat I'm sure that it contains that I want. But I can't do the res.json and the res.render. Because of the two invoke of send function.

And in my angular I have this in a function :

    var resultat = []
    $http.get('/data')
    .success(function(res){
        angular.extend(resultat, res.data);
    })
    .error(function(res){
        console.log('err');
    })
    return resultat;

The goal is to render my index.ejs and to show my planning in this page. But I find no solution to do this. This is my first ask on stackoverflow, english is not my native language. Please don't be rude with me :)

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I'm not familiar with EJS, I use handlebars, but you should be able to pass data in the render function like so-

...
res.render("index", { data:resultat }); 
...

Then access it in the template in whatever format EJS uses. For hbs it would look something like

...
<div>My data looks like this: {{data}}</div>
...

Again, EJS is sure to do it differently, refer to the doc to ensure you have the correct format.

over 4 years ago · Santiago Trujillo Denunciar

0

Thanks for your answer MPawlak !It helped me ! That I want is to send the data with the render like you do yes.

But I want to grab/take this data in my angular Factory (my factory fills the controller, this part works) that I show before :

var resultat = []
$http.get('/data')
.success(function(res){
    angular.extend(resultat, res.data);
})
.error(function(res){
    console.log('err');
})
return resultat;

With your method, I can take this data into my view directly your right and it's works ! Thanks !

<pre> <%= data %> </pre>

So I was thinking about a dirty temporaly solution to do this:

<textarea ng-model="planning"> <%= data %> </textarea>

But when I want to show this planning it don't work and stay empty... I don't understand why.

But to get a good and clean solution I think this is not a good idea, so my ask is the same... how to take this data in my angular factory directly ?

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda