Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.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
Javascript Angularjs:实现与工厂相同的服务结构_Javascript_Angularjs_Factory_Plunker - Fatal编程技术网

Javascript Angularjs:实现与工厂相同的服务结构

Javascript Angularjs:实现与工厂相同的服务结构,javascript,angularjs,factory,plunker,Javascript,Angularjs,Factory,Plunker,我正在玩angularjs服务和工厂。我创建了一个名为BookFactory的工厂,如下所示: // Code goes here app .factory('BootFactory', function() { var bookFactory; function add() { } function remove() { } function getAll() { } bookFactory = {

我正在玩
angularjs
服务
工厂
。我创建了一个名为
BookFactory
的工厂,如下所示:

// Code goes here

app
  .factory('BootFactory', function() {
    var bookFactory;

    function add() {

    }

    function remove() {

    }

    function getAll() {

    }
    bookFactory = {
      add: add,
      remove: remove,
      getAll: getAll
    }
    return bookFactory;

  });
app.service('BookService', function() {

        var bookService;

        function add() {

        }

        function remove() {

        }

        function getAll() {

        }

        /*
        It throws:
        Uncaught ReferenceError: Invalid left-hand side in assignment.

        How can I achieve the same structure with the services which I have used in factories.

        this = {
          add: add,
          remove: remove,
          getAll: getAll
        };

        */

        //Do I have to add all methods like this
        this.add=add;
        this.remove=remove;
        this.getAll=getgAll;

        //Or there's any other better way to achieve it

      });
以及
图书服务
,如下所示:

// Code goes here

app
  .factory('BootFactory', function() {
    var bookFactory;

    function add() {

    }

    function remove() {

    }

    function getAll() {

    }
    bookFactory = {
      add: add,
      remove: remove,
      getAll: getAll
    }
    return bookFactory;

  });
app.service('BookService', function() {

        var bookService;

        function add() {

        }

        function remove() {

        }

        function getAll() {

        }

        /*
        It throws:
        Uncaught ReferenceError: Invalid left-hand side in assignment.

        How can I achieve the same structure with the services which I have used in factories.

        this = {
          add: add,
          remove: remove,
          getAll: getAll
        };

        */

        //Do I have to add all methods like this
        this.add=add;
        this.remove=remove;
        this.getAll=getgAll;

        //Or there's any other better way to achieve it

      });
我想做的是保持结构的一致性,也就是说,我希望工厂和服务都能做到这一点:

bookFactory = {
          add: add,
          remove: remove,
          getAll: getAll
        }
在工厂的情况下,它可以正常工作。但如果是服务的话,我不能这样做。原因服务与
一起工作,我无法执行此操作:

/*
            It throws:
            Uncaught ReferenceError: Invalid left-hand side in assignment.

            How can I achieve the same structure with the services which I have used in factories.

            this = {
              add: add,
              remove: remove,
              getAll: getAll
            };

            */
我想做的是:

//Do I have to add all methods like this
            this.add=add;
            this.remove=remove;
            this.getAll=getgAll;

            //Or there's any other better way to achieve it

有没有更好的办法?这是我的建议

您可以创建bookService,如-

app.service('BookService', function() {
    return {
          add:add,
          remove:remove,
          getAll:getAll
        };

    function add() {

        }

        function remove() {

        }

        function getAll() {

        }

  });

您可以创建bookService,如-

app.service('BookService', function() {
    return {
          add:add,
          remove:remove,
          getAll:getAll
        };

    function add() {

        }

        function remove() {

        }

        function getAll() {

        }

  });

您可以这样做:

app.service('BookService', function() {
    function add() {

    }

    function remove() {

    }

    function getAll() {

    }

    var bookService = {
      add: add,
      remove: remove,
      getAll: getAll
    };

    return bookService;
  });

Angular并不真正关心您的服务回报。工厂与服务只是关于如何调用它,工厂是这样调用的:Factory()和服务是这样调用的:new Service()。在JS中,构造函数可以返回您想要的任何内容。

您可以执行以下操作:

app.service('BookService', function() {
    function add() {

    }

    function remove() {

    }

    function getAll() {

    }

    var bookService = {
      add: add,
      remove: remove,
      getAll: getAll
    };

    return bookService;
  });

Angular并不真正关心您的服务回报。工厂与服务只是关于如何调用它,工厂是这样调用的:Factory()和服务是这样调用的:new Service()。在JS中,构造函数可以返回您想要的任何内容。

工厂和服务只是创建服务对象实例的两种稍有不同的方法

factory函数返回实例本身

服务函数是一个构造函数,它与
new
(或类似的东西)一起用于创建实例。JS中的构造函数可以返回直接实例化的对象实例(而不是默认的
this
,)

这应该让您可以这样做:

  .service('BookService', function() {
    var bookService;

    function add() {}

    function remove() {}

    function getAll() {}

    bookService = {
      add: add,
      remove: remove,
      getAll: getAll
    }
    return bookService;
  })
因此,是的,
工厂
服务
可以以完全相同的方式运行,因为这个JS怪癖


我真的不知道为什么你想要两者都有相同的配方,因为从语义上来说,它们做的是相同的,为什么不使用一个特定的创建所有服务呢?

工厂和服务只是创建服务对象实例的两种稍有不同的方法

factory函数返回实例本身

服务函数是一个构造函数,它与
new
(或类似的东西)一起用于创建实例。JS中的构造函数可以返回直接实例化的对象实例(而不是默认的
this
,)

这应该让您可以这样做:

  .service('BookService', function() {
    var bookService;

    function add() {}

    function remove() {}

    function getAll() {}

    bookService = {
      add: add,
      remove: remove,
      getAll: getAll
    }
    return bookService;
  })
因此,是的,
工厂
服务
可以以完全相同的方式运行,因为这个JS怪癖


我真的不知道为什么你想要两者都有相同的配方,因为语义上它们做的是相同的,那么为什么不使用一个特定的创建所有服务呢?

我们不应该使用服务函数返回任何东西,因为它是构造函数。是的,正确。然而,服务是一个构造函数,它不会阻止我们做额外的工作并返回对象文本。事实上,JavaScript中的构造函数可以返回它们想要的任何内容。因此,我们可以使用服务代码,并以一种与我们的服务代码基本相同的方式编写服务代码factory@HiteshKumar请读这个,明白了。非常感谢:)我们不应该返回服务函数的任何内容,因为它是构造函数。是的,正确。然而,服务是一个构造函数,它不会阻止我们做额外的工作并返回对象文本。事实上,JavaScript中的构造函数可以返回它们想要的任何内容。因此,我们可以使用服务代码,并以一种与我们的服务代码基本相同的方式编写服务代码factory@HiteshKumar请读这个,明白了。非常感谢:)