I have an AngularJS application with a .NET MVC/WebAPI backend. I have one MVC action that serves up my main HTML page that loads my AngularJS app. This MVC action loads several application settings from the Web.config as well as the database and returns them to the view as a model. I'm looking for a good way to set those MVC Model
values as $provide.constant
values in my AngularJS .config
method.
MVC Controller method:
public ActionResult Index() {
var model = new IndexViewModel {
Uri1 = GetUri1(),
Uri2 = GetUri2()
//...etc
};
return View(model);
}
My MVC _Layout.cshtml:
@model IndexViewModel
<!doctype html>
<html data-ng-app='myApp'>
<head>
@Styles.Render("~/content/css")
<script type='text/javascript'>
@if (Model != null) //May be null on error page
{
<text>
var modelExists = true;
var uri1 = '@Model.Uri1';
var uri2 = '@Model.Uri2';
</text>
}
else
{
<text>
var modelExists = false;
</text>
}
</script>
</head>
<body>
<!-- Body omitted -->
@Scripts.Render("~/bundles/angular", "~/bundles/app") //Loads angular library and my application
</body>
app.js:
"use strict";
angular.module('myApp', [])
.config(['$provide' '$window', function ($provide, $window) {
if ($window.modelExists){
$provide.constant('const_Uri1', $window.uri1);
$provide.constant('const_URi2', $window.uri2);
}
}]);
This is a vastly simplified version of my code but I think it illustrates my concern. Is there a better or standard way of doing this that I am overlooking? I don't like the code in my _Layout.cshtml
because I have many more configuration values.