2017-01-19 8 views
0

私は、Springbootに基づいてアプリケーションの静的部分をサーバー側から切り離すために、非常に基本的なサンプルアプリケーションを変更しようとしています。私は、nginx上でアプリケーションのAngularJS静的部分を実行し、Springbootアプリケーションへのリモートサービスコールを行いたいと思います。ここで基本的なAngularJSサービスリクエスト

は、アプリケーションの要旨である...

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を私のコントローラに注入しました。これは私がホストを変更する場所です。しかし、前と後のconsole.logは私に "localhost"と表示されます。

AngularJSを使用してサービスにリモートAPI呼び出しを行うにはどうすればよいですか?私が言ったように、それはおそらく非常に基本的な質問です。

答えて

0

角度アプリからAPI呼び出しをしようとしていますか?そのためには、$ 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. 
    }); 

は、公式ドキュメントを参照してください。

https://docs.angularjs.org/api/ng/service/ $ HTTP