Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I want to use AngularJS non-SPA for my website along with ASP.NET MVC. The authentication happens through Active Directory so I find it best to use ASP.NET MVC for routing and authentication/authorization.

So what I would like to do is to create a _Layout.cshtml page that contains a ng-app definition, then each view derived from _Layout.cshtml would contain a ng-controller definition.

For RESTful data handling I would use a ASP.NET Web API.

The only problem I can think of are the URL query string parameters. If I would like to view some record specific by ID, I would always have to put the ID in a ViewBag in the ASP.NET MVC controller and obtain it in the view like this:

<script>
    var roomID = @ViewBag.roomID
</script>

Is there some better way to do this? Please shed some light. :)

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Updated Answer:

Since you are not using Angularjs routing, you can use ngInit. It will initialize a new variable in your controller's scope and you will be able to access it like:

HTML (inside your controller):

<AnyElement ng-init="roomID = @ViewBag.roomID"></AnyElement>

JavaScript:

var roomID = $scope.roomID;

For more explanations see ngInit documentation.

Original Answer:

In Angularjs you can access the query string parameter very easily like:

var paramValue = $routeParams.yourParameterName;

Or if you want to initialize a JavaScript variable then consider using ngInit. It will initialize a new variable in your controller's scope and you will be able to access it like:

HTML (inside your controller):

<AnyElement ng-init="roomID = @ViewBag.roomID"></AnyElement>

JavaScript:

var roomID = $scope.roomID;

For more explanations see ngInit documentation.

Hope that helps.

share|improve this answer
    
I can't access $routeParams unless I'm using AngularJS built-in routing system, which I'm not. I'm using ASP.NET MVC routing system, so I think I have to go with your second suggestion. :) – Gaui May 13 '14 at 11:53
1  
You are right, I should have read your question more carefully. I am just editing the answer now :). Hope you select it as answer if it helps. – Haider May 13 '14 at 15:45
    
Thank you very much. – Gaui May 13 '14 at 23:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.