0

I am passing a model object to MVC view which uses angularjs controller. In the script, I am accessing the model object and saving the model values to a variable. When the view is loaded, it does not run the script. how can I load the AppSettings variable?

Thank you!

<script>
 var AppSettings = (function (o) {
    return o;
 })(@Html.Raw(JsonConvert.SerializeObject(@Model.AppSettings)));

 alert("app settings = " + AppSettings);
</script>

<form name="payment" ng-submit="send()" ng-controller="PaymentCtrl">
</form>
Jyina
  • 2,530
  • 9
  • 42
  • 80
  • Why don't you do it the angular/REST way, expose the app settings in a REST service returning JSON, and using $http in the controller to load the settings? – JB Nizet May 20 '15 at 20:34
  • @JB Nizet Why make an extra request to the server when you don't have to? I'd prefer the OP method to making an extra call. – Scottie May 20 '15 at 20:37
  • I'm not quite sure what you are trying to accomplish with the settings here... Is this supposed to be some global function you can use to get a setting? – Scottie May 20 '15 at 20:39
  • To keep the application clean: a HTML template is supposed to contain a HTML template, not data. And BTW, it's loaded only once and cached. – JB Nizet May 20 '15 at 20:40
  • possible duplicate of [Angularjs does not load scripts within ng-view](http://stackoverflow.com/questions/18220197/angularjs-does-not-load-scripts-within-ng-view) – JB Nizet May 20 '15 at 20:45
  • I am trying to load the app settings from web.config in MVC controller method and passing them to MVC view. But the intention is to use them in angularjs service so I need the settings in a global variable. – Jyina May 20 '15 at 20:47
  • Ideally, I need the settings to be available when the application is launched so that they will be available across the application. I am not sure how to do that. But for now, I am trying to pass them as model object to a view to access them from that point on. Thanks. – Jyina May 20 '15 at 20:56
  • If you need it everywhere, and from the start of the application, why do you write it in a specific view, and not in the main HTML page (index.html)? – JB Nizet May 20 '15 at 21:00
  • Ok. I could do that. But what should I do to run the script when view is loaded? Thanks! – Jyina May 20 '15 at 21:14
  • Does angularjs ignore the script? – Jyina May 20 '15 at 21:15
  • I see that it requires a reference to jquery to run the script as suggested in your link. Thanks! – Jyina May 20 '15 at 21:28

0 Answers0