0

In an ASP Page I have a AngularJS modules directly applied. The ASP-page get from the server a parameter, that is to be used in the module. Like this (foo is the parameter):

<script>
var myApp = angular.module('myApp', []);

myApp.controller('myController', function($scope ) {
     $scope.foo = @Model.foo;
...

Now I would like to outsource the module in a separate JS file. How can I inject the parameter into the module then?

1 Answer 1

1

Just define parameters in global scope, and use it in angular app.

Index.cshtml:

<!-- this shoud be initialized before angular appplication -->
<script type='text/javascript'>
  window.backendParams = {
    foo: @Model.foo ,
    bar: @Model.bar
  }
</script>

app.js:

myApp.controller('myController', function($scope ) {
     $scope.foo = window.backendParams.foo;
...
Sign up to request clarification or add additional context in comments.

1 Comment

@bogus, look at answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.