使用带有 angular 的 $q 服务

Abd*_*rim 5 angularjs angularjs-http angular-promise

我仍然无法理解使用 $q 服务的作用,(它究竟会添加什么)如果你想创建一个只需要通过 http 调用一个 API 的服务,在这种情况下我不知道为什么应该'我只是执行以下操作(不使用 $q):

this.getMovie = function(movie) {
  return $http.get('/api/v1/movies/' + movie)
    .then(
    function(response) {
      return {
        title: response.data.title,
        cost: response.data.price
      });
    },
    function(httpError) {
      // translate the error
      throw httpError.status + " : " +
        httpError.data;
    });
};
Run Code Online (Sandbox Code Playgroud)

小智 0

$http进行异步调用。因此,理想情况下,如果您想获取 url 中响应的值,您应该使用 '$scope' 变量来存储它。

   $http("your url").then(function(response){
      //This is the success callback
      $scope.movies = response.data;
      });
Run Code Online (Sandbox Code Playgroud)