Javascript 基本AngularJS服务请求
我试图对一个非常基本的示例应用程序进行更改,以便基于Springboot将应用程序的静态部分与服务器端分离。我想在nginx上运行应用程序的AngularJS静态部分,并对Springboot应用程序进行远程服务调用 以下是应用程序的要点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
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