Is it possible to use Razor inside a Javascript file?

0

I have a .js file and would like to use the syntax of Razor inside it. Home I tried to use @ but it did not work. Home Is there any possibility? Can someone help me? I've researched a lot, but I can not find anything ...

    
asked by anonymous 23.04.2015 / 22:56

1 answer

1

You have the bad side, you have to do this right on the page, but it solves the problem.

Example:

Imagine your .js is something like:

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

On your razor page, after you load the .js script you put:

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

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

minhasVariaveis.MinhaVariavelUm
minhasVariaveis.MinhaVariavelDois

And so on ...

    
24.04.2015 / 03:01