title: Using Express to render an .ejs template for AngularJS and use the data inside AngularJS $scope
body: <p>I hope I can explain myself with this first question I post on Stack Overflow.</p>
  
  <p>I am building a small test application with the MEAN stack.</p>
  
  <p>The application receives variable data from Mongoose based on an Express Route I have created.</p>
  
  <p>For example the url is: localhost:3000/cities/test/Paris</p>
  
  <p>Based on the name of the city the response gives me the name of the city and a description. I Know how to get this data inside the .ejs template
  But thats not what I want. I want to use this data inside an ngRepeat.</p>
  
  <p>Maybe this is not the right way but maybe you can help me figure this out.</p>
  
  <p>The reason I want to do this is because I don't want a single page application but an Angular template that can be used over and over for each city and only uses the data that gets back from the mongoose find() results and not the whole cities array.</p>
  
  <p>app.js :</p>
  
  <pre><code>var cityRoutes = require('./routes/cities');
  app.use('/cities', cityRoutes);
  app.set('views', './views'); // specify the views directory
  app.set('view engine', 'ejs'); // register the template engine
  </code></pre>
  
  <p>./routes/cities/cities.js :</p>
  
  <pre><code>var express = require('express');
  var citiesList  = require('../server/controllers/cities-controller');
  
  var bodyParser = require('body-parser');
  var urlencode = bodyParser.urlencoded({ extended: false });
  
  var router = express.Router();
  
  // because this file is a fallback for the route /cities inside app.js
  // the route will become localhost:3000/cities/test/:name
  // not to be confused by its name in this file.
  router.route('/test/:name')
      .get(citiesList.viewTest)
  
   module.exports = router;
  </code></pre>
  
  <p>../server/controllers/cities-controller.js :</p>
  
  <pre><code>var City = require('../models/cities');
  
  module.exports.viewTest = function(request, responce){
  City.find({ stad: request.params.name }, function(err, results){
  
      if (err) return console.error(err);
          if (!results.length) {
              responce.json( "404" );
      } else {
          responce.render('angular.ejs',  { messages:results });
          // through this point everything works fine
          // the angular.ejs template gets rendered correctly
          // Now my problem is how tho get the results from the 
          // response.render inside the Angular directive 
          // so I can use the data in a $scope
      }
      });
  };
  </code></pre>
  
  <p>../models/cities.js</p>
  
  <pre><code>var mongoose = require('mongoose');
  
  module.exports = mongoose.model('City', {
      stad: { type: String, required: true },
      omschrijving: String
  });
  </code></pre>
  
  <p>AngularJS directive :</p>
  
  <pre><code>// This is where I would like to use the messages result data
  // so I can create a $scope that handles data that can be different
  // for each url 
  // so basically I am using this directive as a template
  
  app.directive('bestelFormulier', function () {
  return {
      restrict: 'E',
      templateUrl: '/partials/bestel-formulier.html',
      controller: ['$scope', '$http', '$resource', '$cookieStore',
  function($scope, $http, $resource, $cookieStore){ 
  
  
      // at this point it would be nice that the $scope gets the
      // url based results. But I don't now how to do that..
      // at this point the var "Cities" gets the REST API with 
      // all the cities...
  
      var Cities = $resource('/cities');
  
          // get cities from mongodb
          Cities.query(function(results){
              $scope.cities = results;
              //console.log($scope.products);
          });
  
          $scope.cities = {};
  
      }],
      controllerAs: 'productsCtrl'
  }
  
  });
  </code></pre>
  
  <p>The database is stored like this :</p>
  
  <pre><code>[
      {
          stad: 'Paris',
          omschrijving: 'description Paris',
      },
      {
          stad: 'Amsterdam',
          omschrijving: 'description Amsterdam',
      }
  ]
  </code></pre>
  
  <p>I hope these files included helps explaining my issue.</p>
  
  <p>Thanks in advance for helping me out</p>