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

139
Vistas
ExpressJS + AngularJS: communicate routes and controllers

I have a json object in my express.js routes file and in order to parse it, I am guessing that the best way would be to send to my Angular controller to be looped in my view.

routes/myaccount.js

router.post('/', function(req, res) {
  // form submitted
  var email = req.body.email
  var password = req.body.password
  login.returnSessionToken(email, password, (token) => {
      console.log("return token: ", token)
      var logged = require('../js/index')
      logged.returnUserData(token, (myData) => {
        res.render('myaccount', { myDataFromRoutes:myData });
      })

    },
  (fail) => {console.log(fail)})

})

controller.js

var controllers = angular.module('myApp.controllers', []);

controllers.controller('IndexController', ['$scope', function($scope) {
    $scope.data= myDataFromRoutes; //how do I get the JSON object from my routes and pass it here?
}]);

myaccount.html

<body ng-app="myApp">
    <div ng-controller="IndexController">
        <li ng-repeat="d in data">
          {{ d }}
        </li>
   </div>
</body>

Is the structure right? I can't figure how to make my express routes communicate with my angular controller.

Any help will be much appreciated. Thanks!

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Since you are not using $http to AJAX the JSON object, you can use ngInit. This is one of the few intended uses of ngInit. Use Jade or whatever server template engine you have to interpolate the JSON object in the ng-init directive:

<body ng-app="myApp">
    <div ng-controller="IndexController" ng-init="data = #{JSON.stringify(myDataFromRoutes)}">
        <li ng-repeat="d in data">
          {{d}}
        </li>
   </div>
</body>

Since you are operating inside the controller's scope, the data variable will also be available in your controller as $scope.data.

Alternatively, (probably better, actually) you can interpolate directly in the JavaScript. But this, won't work for JS included as a file. It will have to be an inline script:

<script>
  var controllers = angular.module('myApp.controllers', []);
  controllers.controller('IndexController', ['$scope', function($scope) {
    $scope.data= #{JSON.stringify(myDataFromRoutes)};
  }]);
</script>

NOTE: Recent versions of Pug have replaced the above interpolation syntax with the following:

ng-init="data = `${JSON.stringify(myDataFromRoutes)}`"
about 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