skip to Main Content

I have used Angular build files in WebForms (.aspx) as JavaScript files.

I need to pass some values to Angular which are then used for other API calls.

Right now I’m using HTTP Cookies to pass the values. Is there any other way to pass values other than HttpCookie?

2

Answers


  1. I see two other simple ways:

    • set some initial value in a JavaScript variable with the settings
    • create a "setup" API call, that returns the settings
    Login or Signup to reply.
  2. There are more ways to do this then there is flavors of ice-cream.

    I really don’t know angular much.

    but, say this markup:

        <asp:HiddenField ID="hCustomer" runat="server" ClientIDMode="Static" />
    
        <div style="padding: 35px">
    
            <div ng-app="myApp" ng-controller="myCtrl">
    
                Full Name: {{firstName + " " + lastName}} <br />
                Company: {{Company}} <br />
    
            </div>
    
        </div>
    
        <script>
    
            var Customer = JSON.parse(document.getElementById("hCustomer").value)
    
            var app = angular.module('myApp', []);
            app.controller('myCtrl', function ($scope) {
                $scope.firstName = Customer.FirstName;
                $scope.lastName = Customer.LastName;
                $scope.Company = Customer.Company;
            });
    
        </script>
    

    So, code behind to pass those values?

    This works:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        If Not IsPostBack Then
    
            Dim MyCustomer As New Dictionary(Of String, String)
    
            MyCustomer.Add("FirstName", "Albert")
            MyCustomer.Add("LastName", "Kallal")
            MyCustomer.Add("Company", "Turtle Creek Tours")
    
            Dim JS As New JavaScriptSerializer
            hCustomer.Value = JS.Serialize(MyCustomer)
    
        End If
    
    End Sub
    

    And the result is now this:

    enter image description here

    I used a dictionary in place of creating a separate class, and they are kind of handy, since you can just "cook up" and write out code with as many variables you want to pass to the client side.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search