简体   繁体   English

通过Angular服务使用RESTful响应

[英]Consuming RESTful response via Angular service

I'm following scotch.io's tutorial on building a RESTful API while trying to get familiar with the MEAN stack. 我正在遵循scotch.io的有关构建RESTful API的教程 ,同时尝试熟悉MEAN堆栈。

I've followed pretty much everything so far, and got my RESTful API sending out JSON as intended. 到目前为止,我几乎已经了解了所有内容,并让我的RESTful API可以按预期发送JSON。 Should I try to access it via browser address bar or try it out with Postman it works. 我应该尝试通过浏览器地址栏访问它,还是尝试使用Postman来运行它。

I'm having problems with the consumption of said JSON response. 我在使用上述JSON响应时遇到问题。

According to the tutorial, the Angular app is divided in controllers and services . 根据教程,Angular应用分为控制器服务 The service uses $http to call the RESTful endpoint. 该服务使用$http调用RESTful端点。 My doubt is where and how should I use that service to call for the data. 我的疑问是我应该在哪里以及如何使用该服务来调用数据。

Is it in the controller? 在控制器中吗? Is the service exposed in a way that I can add its response to $scope ? 服务是否以可以将其响应添加到$scope的方式公开?

I'm new to Angular/client-side routing, so please be gentle:) My code is below. 我是Angular /客户端路由的新手,所以请保持柔和:)我的代码如下。

(Blog) Controller: (博客)控制器:

angular.module('BlogCtrl', []).controller('BlogController', function($scope, $http) {

    $scope.tagline = 'Blog page!';

    // can and should I call the service here?

});

Service: 服务:

angular.module('BlogService', []).factory('Post', ['$http', function($http) {

    return {
        // call to get all posts
        get : function() {
            return $http.get('/api/blog');
        }     

}]);

Routes: 路线:

angular.module('appRoutes', []).config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {

    $routeProvider

        // blog page that will use the BlogController
        .when('/blog', {
            templateUrl: 'views/blog.html',
            controller: 'BlogController'
        })        

    $locationProvider.html5Mode(true);

}]);

Angular App: Angular应用程序:

angular.module('myApp', ['ngRoute', 'appRoutes', 'MainCtrl', 'BlogCtrl', 'BlogService']);

Yes, you can make $http call in your BlogController . 是的,您可以在BlogController进行$ http调用。

However if you want to use your 'Post' factory, you should inject it to controller 但是,如果要使用“ Post”工厂,则应将其注入控制器

angular.module('BlogCtrl', []).controller('BlogController', function($scope, Post) {...}

and make the request 并提出要求

Post.get().then(
  function(response){console.log(response.data)},
  function(errorResponse){/*...*/}
);

(I think you should also read about $resource ( https://docs.angularjs.org/api/ngResource/service/ $resource). Maybe it is something what you could use to replace your Post factory ;)) (我认为您还应该阅读有关$resourcehttps://docs.angularjs.org/api/ngResource/service/ $ resource)。也许可以用它来代替Post工厂;)

You want to inject the service into controller ( or anywhere else you would use it) and then make the function call using the injected service object 您想将服务注入控制器(或您将在其他任何地方使用),然后使用注入的服务对象进行函数调用

angular.module('BlogCtrl', [])
    .controller('BlogController', function($scope, Post) {

    $scope.tagline = 'Blog page!';

    // Use service to get data
    Post.get().then(responsePromise){
     $scope.someVariable = responsePromise.data;
    }).catch(function(err){
       console.warn('Ooops error!')
    });        

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM