0

I'm writing an ASP.NET application with AngularJS. I have a value that can only be accessed from the codebehind of a page. I'd like to pass this value down so it can be used by an AngularJS controller. I know it's possible to grab URL parameters for use in AngularJS, but I haven't been able to find any other way of getting a value into my controller. Is this possible?

For example, if I have a setup like this:

<%= user.UserDescription %> <%-- this is how I'd get my value from the codebehind --%>
<div ng-app='myApp' ng-controller='myController1'></div>
...
</div>

I want myController1 to have access to user.UserDescription. It's obviously possible to place the value anywhere on the page, by embedding the C# in the ASP.NET page as you see above, but is it possible to pass it into the controller?

1 Answer 1

1

I had a similar issue as I had sever side config that I wanted the client side application to know. The solution I came up with is was a config constant that I would add to the bottom of the page. Then loaded that into my services, etc....

Something like this

<html>
    <head>
        ....
    </head>
    ....

    <body>
        <script>
            (function(){
              'use strict';
              angular.module('myapp.config',[])
                .constant('CONFIG', {
                  user: {
                        description: '<%= user.UserDescription %>'
                    }
                });
            })();
        </script>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

3 Comments

Then how can you call that from the controller without it yelling at you that it doesn't exist?
Without knowing that, it seems to just be "undefined" when called from any controller.
plnkr.co/edit/OgsNzAHhW7shaf4yVVaf?p=preview simple example of what I am referring to.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.