Using a Relative Path for a Service Call in AngularJS

后端 未结 9 2039
别跟我提以往
别跟我提以往 2020-11-28 03:51

I have the following code, which was working fine until I deployed to a test server:

$scope.getUserList = function (userName) {
    $http({
        method: \         


        
相关标签:
9条回答
  • 2020-11-28 04:21

    Use the $location service - it will return your path, the hash, the server address.. Everything you need! Your call would be to $location.path()+"/GetUserList" or something similar.

    See here: http://docs.angularjs.org/guide/dev_guide.services.$location

    0 讨论(0)
  • The accepted answer helped me. I'm using Angular served up my an MVC app. I took one extra step so that my baseUrl could be used within my angular controllers for web api calls or for accessing templates.

    Using ng-init to set the baseUrl (from a server side populated value)

    <html ng-app="app" ng-controller="AppController">
    <head>
        <base href="{{baseUrl}}" ng-init="baseUrl = '@Model.BaseUrl'" />
    </head>
    

    Angular controller

        $scope.manageCustomerGroups = function () {
            openDialog($scope.baseUrl + 'content/templates/customerGroups.html');
        }
    
    0 讨论(0)
  • 2020-11-28 04:23

    A simple way, I'm using ASP.NET Razor (Web MVC), so I get the Application path and make it as the base of app.

    <head>
    
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Title</title>
    
    <meta name="description" content="">
    @{ var appPath = Request.ApplicationPath.ToString();
        if (!appPath.EndsWith("/")) {
            appPath = appPath + "/";
        }
    }
    <base href="@appPath" />
    
    0 讨论(0)
提交回复
热议问题