user3111525
user3111525

Reputation: 5213

How to use a JS variable which is passed to ejs template for angular app?

Given an EJS template which is rendered with expressJS, I have a variable itemId. How can I use it in the MyCtrl controller?

<script>x="<%=itemId%>"</script>
<div ng-controller="MyCtrl">

</div>

So far I have tried

<script>$scope.x=<%=itemId%></script>

and then in the controller, try to fetch it with $scope.x but it does not work.

Upvotes: 1

Views: 3499

Answers (1)

TheSharpieOne
TheSharpieOne

Reputation: 25726

You may need to add quotes around your var if it is a string or zero padded number.

<script>x="<%=itemId%>";</script>
<div ng-controller="MyCtrl">

</div>

Also, you need to set it to your $scope'd value (in your controller). Assuming you have your controller in another JS file somewhere else in your code

function MyCtrl($scope){
    $scope.x = window.x;
}

Upvotes: 7

Related Questions