Angular.jsのServiceとFactoryに関して
ServiceとFactoryの違い
Serviceは、thisがあるが、returnがない。
// ---------------------------- // Service // ---------------------------- (function() { 'use strict'; angular .module('weatherApp') .service('exampleService', exampleService); function exampleService() { // public var var foo; // private var var bar; // public service // FACTORY HARUS ADA THIS // TIDAK ADA RETURN this.foo = foo; this.doSomething = doSomething; // public method function doSomething () { cook(); console.log("exampleService"); } // private method function cook () { bar = 1; foo = 3; } } })();
Factoryは、returnがあるが、thisがない。
// ---------------------------- // Factory // ---------------------------- (function() { 'use strict'; angular .module('weatherApp') .factory('exampleFactory', exampleFactory); function exampleFactory() { // public var var foo; // private var var bar; // public service // FACTORY HARUS ADA RETURN // TIDAK ADA THIS return { foo: foo, doSomething: doSomething }; // public method function doSomething () { cook(); console.log("exampleFactory"); } // private method function cook () { bar = 1; foo = 3; } } })();