Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基本AngularJS服务请求_Javascript_Angularjs_Api_Service - Fatal编程技术网

Javascript 基本AngularJS服务请求

Javascript 基本AngularJS服务请求,javascript,angularjs,api,service,Javascript,Angularjs,Api,Service,我试图对一个非常基本的示例应用程序进行更改,以便基于Springboot将应用程序的静态部分与服务器端分离。我想在nginx上运行应用程序的AngularJS静态部分,并对Springboot应用程序进行远程服务调用 以下是应用程序的要点 angular.module('blah', ['ent1', 'errors', 'status', 'info', 'ngRoute', 'ui.directives']). config(function ($locationProvider, $rou

我试图对一个非常基本的示例应用程序进行更改,以便基于Springboot将应用程序的静态部分与服务器端分离。我想在nginx上运行应用程序的AngularJS静态部分,并对Springboot应用程序进行远程服务调用

以下是应用程序的要点

angular.module('blah', ['ent1', 'errors', 'status', 'info', 'ngRoute', 'ui.directives']).
config(function ($locationProvider, $routeProvider) {

    $routeProvider.when('/errors', {
        controller: 'ErrorsController',
        templateUrl: 'assets/templates/errors.html'
    });
    $routeProvider.otherwise({
        controller: 'MyController',
        templateUrl: 'assets/templates/albums.html'
    });
}
);

function MyController($scope, $modal, $location, $http, a, b, st) {


$scope.init = function() {
    console.log($location.host());  
--->        $location.host("http://somewhereelse:8080/");
    console.log($location.host());
    ....
};
}
为了便于阅读,删除了一些细节

正如您所看到的,我已经将
$location
注入到我的控制器中,我假设在这里我将更改主机。但是,before和after console.log仍然显示“localhost”


如何使用AngularJS对服务进行远程API调用?正如我所说,这可能是一个非常基本的问题。

希望您正在尝试从您的angular应用程序进行一些API调用。为此,您可以使用angular$http服务

示例代码如下所示:

    // Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });
有关更多详细信息,请参阅官方文件:

$http