Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

345
Visualizações
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 :)

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

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.

about 4 years ago · Santiago Trujillo Relatório

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 ?

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda