0

I have a view rendering written in .cshtml. The .cshtml in turn refers .js files (written as angular2 apps). I want to pass a parameter from the .cshtml to the angular2 app. How do I do this? I am using angular-cli to generate the angular2 app.

user3547774
  • 1,621
  • 3
  • 20
  • 46
  • 1
    Possible duplicate of [How to pass parameters rendered from backend to angular2 bootstrap method](http://stackoverflow.com/questions/37611549/how-to-pass-parameters-rendered-from-backend-to-angular2-bootstrap-method) – Gatogordo Oct 24 '16 at 08:07

2 Answers2

0

I would just output the value in a JavaScript namespace inside a script tag in your view/controller rendering and then use that in your Angular app. Eg. output this:

<script type="text/javascript">
  var solution = solution || {};
  solution.vars = solution.vars || {};
  solution.vars.myVariable = '@Model.MyVariable';
</script>

Then you can just access the solution.vars.myVariable in your other JavaScript.

Richard Seal
  • 4,248
  • 14
  • 29
Jens Mikkelsen
  • 2,712
  • 19
  • 23
0

Jens answer will definitely work. You could also pull the data with a separate call through Sitecore Service Client in Angular if you are concerned with adding variables to global scope or don't want to have additional dependencies on the view. This will have a small performance impact as an additional request would need to be made.

Jeroen
  • 3,443
  • 2
  • 13
  • 15