在控制器內部使用 http

$http 服務是一個生成 HTTP 請求並返回 promise 的函式。

一般用法

// 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.
  });

在控制器內使用

appName.controller('controllerName',
    ['$http', function($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 服務也有快捷方法。在這裡閱讀 http 方法

句法

$http.get('/someUrl', config).then(successCallback, errorCallback);
$http.post('/someUrl', data, config).then(successCallback, errorCallback);

快捷方法

  • $ http.get
  • $ http.head
  • $ http.post
  • $ http.put
  • $ http.delete
  • $ http.jsonp
  • $ http.patch