Is it possible to use Razor inside Javascript file?

Asked

Viewed 505 times

0

I have a file .js and would like to use the syntax of Razor within it.
I tried to simply use the @ but it didn’t work.
Is there any possibility? Can anyone help me? I’ve done a lot of research, but I can’t find anything...

  • Have you tried using Razorjs? http://nuget.org/List/Packages/RazorJS

1 answer

1


Straight into the . js has no way, what you can do is instantiate your class in html/Razor, so you can set values in javascript and pass as class parameter for example...

There’s a downside, you have to do it right on the page, but it solves the problem.

Example:

Imagine that your . js is something like:

var MeuObjetoJs = function(minhasVariaveis){
  //Faz o que tem que fazer...
}

Ai on your page, after vc load the script . js you put:

<script>
  var minhasVariaveis = {
    minhaVariavelUm: '@Model.MinhaVariavelUm',
    minhaVariavelDois: '@Model.MinhaVariavelDois'
  }
  var meuObjetoJs = new MeuObjetoJs(minhasVariaveis);
</script>

Ai in your . js, you would access normally with:

minhasVariaveis.MinhaVariavelUm
minhasVariaveis.MinhaVariavelDois

And so on and so forth...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.