Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 角度:在一个服务中具有多个功能_Angularjs_Service - Fatal编程技术网

Angularjs 角度:在一个服务中具有多个功能

Angularjs 角度:在一个服务中具有多个功能,angularjs,service,Angularjs,Service,是否可以在一个服务中具有多个功能 我的图书服务中有: (function() { 'use strict'; angular .module('app.core') .service('BookService', BookService); BookService.$inject = ['$resource']; /* @ngInject */ function BookServic

是否可以在一个服务中具有多个功能

我的图书服务中有:

(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) {})

是的,你可以。只需定义服务中的函数。最后,返回一个对象,该对象包含要向服务使用者公开的所有函数。编辑:这适用于工厂。有关服务,请参见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
            };

        }
})()

使用服务时,您可以将功能附加到此。这样,您可以在一个服务中拥有多个功能。例如:

(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的方式返回该对象更有意义,那么就使用工厂而不是服务。

这种返回对象的方法就是工厂的作用。你说得对!当我读到你的答案时,我自己就想到了。我将把它改为factoryOk,所以我只需要将BookService.get({id:2},函数(book){})替换为BookService.getById(id).get({id:2},函数(book){})?对吗?只要$scope.book=BookService.getById(id);可以,不需要回拨我需要回拨,因为我不仅有.get,还有其他参数,如.query、.save。请参阅我的更新问题我尝试了BookService.fun1({id:2},函数(book){scope.book=[book];console.log(scope.book);});但是现在我的Book是空的,而我的首字母不是空的code@user708683您的
书籍
可能为空,因为此服务的范围与控制器的范围不同。如果要在函数中使用book,请尝试将其作为fun1的参数传入。