Friday, 28 April 2017

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 :)



via Krakito

No comments:

Post a Comment