Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

342
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!