【问题标题】:Angular : Have multiple functions in one ServiceAngular:在一项服务中具有多种功能
【发布时间】:2017-11-06 17:54:38
【问题描述】:

是否可以在一项服务中拥有多种功能?

我的图书服务中有这个:

(function() {
    'use strict';
    angular
            .module('app.core')
            .service('BookService', BookService);

        BookService.$inject = ['$resource'];
        /* @ngInject */
        function BookService($resource) {
            return $resource('/api/book/:id', {
                id: '@id'
            }, {
                'get': {
                    method: 'GET',
                    cache: true
                },
              'query': {
                method: 'GET',
                isArray: true,
                cache: true
            },
            });
        }
})()

但在同一个服务中,我希望有另一个函数,我将传递其他参数,例如:

 return $resource('/api/book/:name', {
                name: '@name'
            }, {
                'get': {
                    method: 'GET',
                    cache: true
                },
               'query': {
                method: 'GET',
                isArray: true,
                cache: true
            },
            });

我的控制器看起来像这样并且有两个不同的调用:

BookService.get({
                id: 2
            }, function(book) {})

BookService.get({
                name: "bookTitle"
            }, function(book) {})

【问题讨论】:

    标签: angularjs service


    【解决方案1】:

    是的,你可以。只需定义服务中的功能。最后,返回一个对象,其中包含您要向服务消费者公开的所有功能。编辑:这适用于工厂。有关服务,请参阅 nathan 的回答。

    (function() {
        'use strict';
        angular
            .module('app.core')
            .factory('BookService', BookService);
    
            BookService.$inject = ['$resource'];
            /* @ngInject */
            function BookService($resource) {
                var getById = function(id){
                    return $resource('/api/book/:id', {
                        id: id
                    }, {
                        'get': {
                            method: 'GET',
                            cache: true
                        }
                    });
                };
    
                var getByName = function(name) {
                    return $resource('/api/book/:name', {
                        name: name
                    }, {
                        'get': {
                            method: 'GET',
                            cache: true
                        }
                    });
                };
    
                return {
                    getById: getById,
                    getByName: getByName
                };
    
            }
    })()
    

    【讨论】:

    • 这种返回对象的方法就是工厂的用途。
    • 你是对的!当我阅读您的答案时,我自己就想到了。我将其更改为工厂
    • 好的,所以我只需要将 BookService.get({ id: 2 }, function(book) {}) 替换为 BookService.getById(id).get({ id: 2 }, function (书) {}) ?对吧?
    • 只需 $scope.book = BookService.getById(id);可以,不需要回调
    • 我需要一个回调,因为我不仅有 .get,而且还有其他参数,如 .query、.save。等查看我更新的问题
    【解决方案2】:

    使用服务时,您可以将函数附加到this。这样,您可以在一项服务中拥有多种功能。例如:

    (function() {
    'use strict';
    angular
            .module('app.core')
            .service('BookService', BookService);
    
        BookService.$inject = ['$resource'];
        /* @ngInject */
        function BookService($resource) {
            this.fun1 = function(){
                return $resource('/api/book/:id', {
                    id: '@id'
                }, {
                    'get': {
                        method: 'GET',
                        cache: true
                    }
                });
            }
            this.fun2 = function(){
                return $resource('/api/book/:name', {
                    name: '@name'
                }, {
                    'get': {
                        method: 'GET',
                        cache: true
                    }
                });
            }
        })()
    

    然后您可以使用BookService.fun1()Bookservice.fun2() 访问这些函数

    如果将函数附加到一个对象,然后以 fikkatra 所做的方式返回该对象更有意义,那么使用工厂而不是服务。

    【讨论】:

    • 我试过 BookService.fun1({ id: 2 }, function(book) { scope.book= [book]; console.log(scope.book); });但是现在我的书是空的,而我的初始代码不是空的
    • @user708683 您的book 可能为空,因为此服务的范围与您的控制器的范围不同。如果您想在函数中使用 book,请尝试将 book 作为 fun1 的参数传入。
    猜你喜欢
    • 1970-01-01
    • 2017-11-24
    • 2021-01-29
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    相关资源
    最近更新 更多