Basic AngularJS template for Aspx

I have not made the big jump to Angular 8 so I am still using AngularJS.  Here is my basic template to get started with a simple .net aspx page.

Here is my HTML:

    <div ng-app="app" ng-controller="BasicTemplateController" class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">Basic Template</div>
            <div class="panel-body">

				{{myData}}

            </div>
        </div>
    </div>

 

Here is my JS:

    <script type="text/javascript">

        (function () {
            'use strict';

            angular
                .module('app', [])
                .factory('basicTemplateDataService', ['$http', basicTemplateDataService])
                .controller('BasicTemplateController', ['$scope', 'basicTemplateDataService', basicTemplateController]);

            function BasicTemplateController($scope, basicTemplateDataService, $routeParams) {
                $scope.LoadingData = false; 
				$scope.myData = [];

                //methods
                $scope.DoSomething = doSomething;
              
                function doSomething() {
                    basicTemplateDataService.getSomething()
                        .then(function (response) {
                            $scope.myData = response.data.d;
                        })
                }

                function init() {
                    doSomething();
                }
                init();
            }

            function basicTemplateDataService($http) {
                var fac = {};                               //using factory method i am configuring service.
                var svcUrl = '<%= Page.ResolveUrl("~/basicTemplates.aspx")%>';
                fac.getSomething = function () {
                    return $http.post(svcUrl + '/getSomething', {});
                }
                return fac;
            }
        })();
    </script>

 

The data service factory can point to any endpoint.  I tend to create pageMethods in the code behind of the aspx page as such:

	  <WebMethod>
    Public Shared Function getSomething(id As Integer, currentUserID As Integer) As String
        Dim res As String
		
		res = "Yes Sirrrr"

        Return res
    End Function

 

Add comment