0

I'm tring to send backend by ejs to the frontend , controller of angularjs

app.js
i pass data to view/indexcontens.

var users = [
  {
    name: 'john',
    email: 'john@email.com'
  },
  {
    name: 'peter',
    email: 'peter@email.com'
  },
  {
    name: 'max',
    email: 'max@email.com'
  }
];

app.get('/partials/indexContents.html', function(req, res) {
  res.render('partials/indexContents', {users:users})
});
app.get('/partials/about.html', function(req, res) {
  res.render('partials/about')
});

views/partials/indexContents
i made it global variable.

  <div>
    <h3>home</h3>
    <p>wecome to my blog. I hope you enjoy the content!</p>
    <ul>
        <li ng-repeat="post in blogposts">{{post}}</li>
    </ul>
  </div>
        <script>
             users = "<%=abc=users[0].name%>";
        </script>

public/js/contollers.js
trying to get the global variable

var app= angular.module('blog',['ngRoute']);

app.config(function($routeProvider) {
    //set up routes
    $routeProvider
    .when('/home',{
        templateUrl:'partials/indexContents.html',
        controller:'SimpleController'
    })
    .when('/about',{
        templateUrl:'partials/about.html',
        controller:'AboutController'
    })      
    .otherwise({
        redirectTo:'/'
    });
});


app.controller('SimpleController', function($scope){

    $scope.blogposts=[
    'blog post1',
    'blog post2',
    window.users
    ];
}); 


app.controller('AboutController', function($scope){
    $scope.name="graum";
    $scope.bio="i just made it";
});

but it only display blank instead of 'john'.

Please give me a solution, thank you

1 Answers1

0

Why dont you use $resource to get the name from server? I know this is not the best option and certainly not what you are looking for but this way it works for sure. however you can try this too

<script>
         (function() {users = "<% users[0].name%>";})();
</script>
Muhammad Faizan
  • 1,709
  • 1
  • 15
  • 37